Download presentation
Presentation is loading. Please wait.
Published byKelly Morris Modified over 9 years ago
1
Language Elements 1
2
Data Types 2
3
Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical 3
4
See page 105 in the text for additional numerical data types. Double 8 14-15 ± 1.8x10 -324 to ± 4.9x10 308 Single 4 6-7 ±1.4x10 -45 to ±3.4x10 38 Long 8 19-20 -92,233,720,368,547,755,808 to 92,233,720,368,547,755,807 Byte 1 2-3 0 to 255 Short 2 4-5 -32,768 to 32,767 Integer 4 9-10 -2,147,483,648 to 2,147,483,647 Decimal 16 28-29 ± 7.9x10 24 4
5
Textual Char 2 1 (Unicode) Variable depends on Length platform 0 to approx. 2 billion String There are also fixed length Strings which we will examine later. ASCII codeASCII code is in the first 256 characters of Unicode 5
6
Logical Boolean 2 True or False 6
7
Other Date/ 8 midnight Jan. 1, 0001 Time to Dec. 31, 9999 Object 4 or 8 any OBJECT reference User- varies Depends on user Defined 7
8
Identifiers 8
9
Non-Object Naming Must begin with a letter. Must contain only letters, numbers, and the underscore character (_). Punctuation characters and spaces are not allowed. Must be no longer than 255 characters 9
10
Variables First letter is lowercase and first letter of each word is uppercase - firstName Constants First letter of each word is uppercase - NoOfStudents Functions/Subroutines Begin with a verb and first letter of each word is uppercase - FindStudentRecord 10 Non-Object Naming
11
Variables 11
12
Single Double Decimal Byte Integer Long String Object Boolean Date Dim/Public/Private … As Private age As Integer 12 Dim valid As Boolean valid = False Public streetAddress As String Dim valid As Boolean = False
13
13 Arrays You have had experience working with arrays of simple and structured data in previous courses, so my only comment is that Basic uses ( ) to delimit the subscripts and not [ ] that you may have been using. 13 Dim age(100) As Integer Creates an array of integers Age(0) – Age(100) Dim age(99) … Age(0) – Age(99) 99 Dim grades(24,9) As Single 10 columns 25 rows 09 0 24 In VB, most indexed Objects begin with 0 In VB, most indexed Objects begin with 0 Dim names() As String
14
Although Visual Basic does not require variable declarations, it is a moot point, since I require that you declare all variables by placing the Option Explicit statement in the first line of your code. Option Explicit On See page 107 in the text for Dim variations. 14
15
In addition to local and global variables, VB (like c and c++) has an additional one called. 15
16
Private Sub cmdDIM_Click(…)… Dim x As Integer x = x + 1 lblDim.Text = x End Sub Private Sub cmdStatic_Click(…)… Static x As Integer x = x + 1 lblStatic.Text = x End Sub x should be initialized x should be initialized 16 Run Program View Code
17
Constants 17
18
Const MaxStudents As Integer = 100 Public Const State As String = “New York” Constants are declared in the following ways. 18
19
Operators 19
20
^ power operator: 2^3 = 8 \ integer division: 26\4 = 6 (whereas 26/4 = 6.5) Mod modulus (remainder): 26 Mod 4 = 2 Arithmetic Operators The standard operators that are found in most computer languages are also found in VB. The only three you may not have seen are: 20 For a list of additional arithmetic operators, see page 109 in the text.
21
Although VB allows shortcut assignment operators like total += 100 we will NOT use them! Instead we will use “longhand” total = total + 100 Arithmetic Operators 21
22
VB allows you to concatenate (put together) strings with the string concatenation operator &. String Operators 22 "cat" & "nip" = "catnip” "12" & "34" = ____ (Note: some languages, including VB, use the + to indicate Concatenation. How would the following be interpreted?) "cat" + "nip" = "12" + "34" = "12" + 34 = "12" & 34 = (Note: some languages, including VB, use the + to indicate Concatenation. How would the following be interpreted?) "cat" + "nip" = "12" + "34" = "12" + 34 = "12" & 34 =
23
For a list of additional relational operators, see page 147 in the text. Relational Operators The standard operators that are found in most computer languages are also found in VB: = <> > < >= <= 23
24
The last two are new to VB.Net and perform “short-circuiting.” Logical Operators The standard operators that are found in most computer languages are also found in VB: And Or Not AndAlso OrElse 24
25
Unlike c++, Visual Basic does automatic casting (converting mixed data types). However, I require that you disable this by placing the Option Strict statement in the second line of your code. Option Explicit On Option Strict On As shown in the text, you can use functions or method to make the conversions. 25
26
Dim age As Integer age = CInt(txtAge.Text) You can use casting in: Assignment - Dim x As Decimal x = Convert.ToDecimal(txtSalary.Text) In context - If (CInt(txtCount.Text) < 10) Then … Microsoft Help - Conversion Functions Microsoft Help - Conversion Methods 26 Dim statements - Dim age As Integer = CInt(txtAge.Text)
27
Subroutines & Functions 27
28
1.You understand the difference between passing parameters by value (copy of the data) and reference (address of the data). 2.You know the difference between “formal” parameters (must be variables) and “actual” parameters (can be expressions). 3.You understand the concept of “scope.” 4.You know the difference between local and global variables. 28 Assumptions:
29
5.You know the difference between a subroutine (sub) procedure and a function procedure. 6.You know the difference between calling a subroutine and calling a function. 7.You know when/why you should use a subroutine/function. 29 Assumptions: Therefore, we will focus on how subroutines and functions are implemented in VB!
30
30 [Private | Public] Sub ProcedureName ([parameterlist]) statements [Exit Sub] [statements] End Sub Subroutines Parameterlist Syntax: [ByVal | By Ref] variableName As type Default Subroutine Call: [Call] [Me.] ProcedureName ([argumentList])
31
Public Sub ClearTextBoxes() txtPrin.Text = “” txtRate.Text = “” txtTime.Text = “” End Sub 31. ClearTextBoxes(). See pages 173 & 175 in the text for additional examples of defining and calling subroutines. Subroutines
32
32 [Private | Public] Function FunctionName ([parameterlist]) [As type] statements [Exit Function] [Return expression] [statements] End Function Functions Function Call: variable = [Me.] FunctionName ([argumentList]) Option Strict On REQUIRES this! Option Strict On REQUIRES this! Actually as long as FunctionName ([argumentList]) is used properly in “context,” a function call is initiated.
33
Private Function FindInterest( ByVal p As Decimal, ByVal r As Decimal, ByVal t As Decimal) FindInterest = p*r/100D*t End Function 33. If(FindInterest(principal,rate,3D)< 1000D) Then.... See pages 178 - 180 in the text for additional examples of defining and calling functions. Functions Principal and rate would be dimensioned as? As Decimal Return p*r/100D*t
34
Programming Constructs Sequence RepetitionSelection 34
35
Selection Selection Conditional Statements 35
36
If statement(s) If (cond) Then S 1 If (Rate > 65) Then FineMsg = "You owe $75.00" If (cond) Then S1 Else S2 If (Grade < 60) Then lblmsg.Text = "failing" Else _ lblMsg.Text = "passing" If (cond) Then statement(s) [Else statement(s)] End If [ ] denote optional statements 36 Unlike c/c++ and Java, no { } are needed to delimit a block of statements! The Then, Else, or EndIf are the delimiters.
37
Using multiple If statements, write the code that would implement the following grading scale: 100 - 90 A 89 - 80 B 79 - 70 C 69 - 60 D 59 - 0 E (Assume that the variables NGrade and LGrade have been appropriately declared. How is this done, and what types would have been used?) 37
38
ElseIf statement If (cond1) Then statement(s) ElseIf (cond2) Then statement(s) ElseIf (cond3)...... [Else statement(s)] End If Note: ElseIf is one word but End If are two words like End Sub and End Class. If you mistype either, VB will automatically correct it for you!!! Note: ElseIf is one word but End If are two words like End Sub and End Class. If you mistype either, VB will automatically correct it for you!!! 38
39
Select Case statement Select Case var (or property) Case expression1 statement(s) Case expression2 statement(s) [Case Else statement(s)] End Select 39
40
In the next example, we are using the same button, which can have one of two “texts”. This is similar to what is done in the iTunes player, except the button has graphics instead of text. The Play button changes from to the Pause button. Depending on the graphic, a different action is performed when the button is clicked. 40
41
Now using the Select Case statement, we will determine what action to take, based on the text of a specific command button. 41
42
Select Case cmdButton.Text Case "Start" statement(s) cmdButton.Text = "Stop" Case "Stop" statement(s) cmdButton.Text = "Start" End Select The code would be: Be careful, even though VB is not case sensitive, strings are !!! Be careful, even though VB is not case sensitive, strings are !!! 42
43
Repetition Repetition Looping Statements 43
44
Looping Statements Enumerated Pre-test Post-test 44
45
For var = beg To end [Step step] statement(s) Next var Enumerated For Counter = 1 To 10... Next Counter 45 If we were indexing an array, we would do: 0 To 9 If we were indexing an array, we would do: 0 To 9
46
Do While (cond) statement(s) Loop Pre-test Do While (Len(txtLastName.Text) = 0)... Loop 46 What is this testing? You could also test: txtLastName.Text = “” You could also test: txtLastName.Text = “”
47
Post-test Do Until (Temperature < 0)... Loop 47 Do Until (cond) statement(s) Loop
48
Read the Description Of Assigment-2 48
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.