vb_variables.docx
TRANSCRIPT
-
7/30/2019 VB_Variables.docx
1/6
Visual Basic Variable
Variable: Provide temporary storage of data. the variables are assigned names by the programmer when
they are declared so that they can easily be referenced in other places in the application code.
Data type: Define the type of the data that will be stored on variable
Byte
Integer
Long
Decimal
Double
Char
String
Date
Boolean
Variables Declaration
Variables are declared using the Visual Basic Dim keyword. The syntax for a simple declaration of a
variable is as follows:
DimvariableNameAsvariableType
Note: Dim is the keyword which indicates to Visual Basic that a variable is being declared.
scope of a variable
The scope of a variable relates to where else in the application source code the variable is accessible.
Local variables are recognized only in the procedure in which they are declared. They can be declared
with Dim and Statickeywords.
A module level variable is available to all the procedures in the module. They are declared using
the Publicor the Private or DIM keyword.
Array Variable
An array is a consecutive group of memory locations that all have the same name and the same
type.
To refer to an element in the array, we specify the array name and the array element position
number it is called Index
-
7/30/2019 VB_Variables.docx
2/6
Declaration
Dim list(10) as integer declares a 10-element array, indexed 0 to 9
Dim sortList(1 To 10) as integer declares a 10-element array, indexed 1 to 10
List(0)=10
List(1)=2
List(list(1))= List(0) + List(1) after execution List(2)=12
Control Array
Similar to arrays of variables, you can group a set of controls together as an array.
The set of controls that form a control array must be all of the same type (all textboxes,all labels, all option buttons, etc.)
The properties of the controls in the control array can vary
To refer to a member of a control array:
ControlName(Index).Property
Ex:
txtOperator(1).Text = +
Event Procedure of control Array
Consider command button = cmdOperator
Private Sub cmdOperator_Click(Index As Integer)
Where parameter Index refer index of control that is clicked
End Sub
Private Sub txtField_KeyPress(Index As Integer, KeyAscii As Integer)
End Sub
-
7/30/2019 VB_Variables.docx
3/6
Control Statement
Control Statements are used to control the flow of program's execution.
1) If...Then..Else selection structure
The If...Then selection structure performs an indicated action
Syntax
If Then
statement1
statement2
End If
If Thenstatements1.1
statements1.2
Else
statements2.1
statements2.2
End If
Eg:
Dim marks as integermarks=45If marks >= 40 Then
Print PassElse
Print FailEnd If
2) Nested If Statement
If < condition 1 > Then
statementsElseIf < condition 2 > Thenstatements
ElseIf < condition 3 > Thenstatements
Else
Statements
End If
-
7/30/2019 VB_Variables.docx
4/6
Eg:
Dim marks as integermarks=val(txtMarks.Text)If marks >=70 Then
Print Grade is A
Else If marks >=60 ThenPrint Grade is BElse If marks >=50 Then
Print Grade is CElse If marks >=40 Then
Print Grade is S
ElsePrint Grade is F
End If
3) Select Case Structure
Syntax
Select Case expression
Case value1
Block of one or more VB statementsCase value2
Block of one or more VB Statements
Case value3
..
Case ElseBlock of one or more VB Statements
End Select
Eg:
grade=txtgrade.Text
Select Case gradeCase "A"
result.Caption="High Distinction"Case "A-"
result.Caption="Distinction"Case "B"
result.Caption="Credit"Case "C"
result.Caption="Pass"
-
7/30/2019 VB_Variables.docx
5/6
Case Else
result.Caption="Fail"End Select
Loop
1)The For...Next Loop
The For...Next Loop is one of the way to make loops in Visual Basic.
Dim x As IntegerFor x = 1 To 50
Print x
Next x
In order to count the numbers from 1 yo 50 in steps of 2, the following loop can be used
For x = 1 To 50 Step 2Print x
Next x
2)Do While... Loop Statement
The Do While...Loop is used to execute statements until a certain condition is met. The following DoLoop counts from 1 to 100.
Dim number As Integernumber = 1
Do While number
-
7/30/2019 VB_Variables.docx
6/6
Graphics Method
The Pset Method
The Pset method draw a dot on the screen
Syntax
Pset (x , y ), color
Eg1:
For x= 0 to 50Pset(x,x)Next x
Eg2:
For x= 0 to 100Pset(x,x) , vbMagentaNext x
Eg3:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
Pset(x,y)
End sub
Other Method
Line (x1, y1)-(x2, y2), color
Circle (x1, y1), radius, color