Sei sulla pagina 1di 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: Dim variableName As variableType 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 Static keywords. A module level variable is available to all the procedures in the module. They are declared using the Public or 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

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

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 <condition> Then statement1 statement2 End If If <condition > Then statements1.1 statements1.2 Else statements2.1 statements2.2 End If Eg: Dim marks as integer marks=45 If marks >= 40 Then Print Pass Else Print Fail End If

2) Nested If Statement
If < condition 1 > Then statements ElseIf < condition 2 > Then statements ElseIf < condition 3 > Then statements Else Statements End If

Eg: Dim marks as integer marks=val(txtMarks.Text) If marks >=70 Then Print Grade is A Else If marks >=60 Then Print Grade is B Else If marks >=50 Then Print Grade is C Else If marks >=40 Then Print Grade is S Else Print Grade is F End If

3)

Select Case Structure

Syntax
Select Case expression Case value1 Block of one or more VB statements Case value2 Block of one or more VB Statements Case value3 . . Case Else Block of one or more VB Statements End Select

Eg:
grade=txtgrade.Text Select Case grade Case "A" result.Caption="High Distinction" Case "A-" result.Caption="Distinction" Case "B" result.Caption="Credit" Case "C" result.Caption="Pass"

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 Integer For 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 2 Print 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 Do Loop counts from 1 to 100. Dim number As Integer number = 1 Do While number <= 100 number = number + 1 Loop

Graphics Method

The Pset Method The Pset method draw a dot on the screen Syntax Pset (x , y ), color Eg1:
For x= 0 to 50 Pset(x,x) Next x Eg2: For x= 0 to 100 Pset(x,x) , vbMagenta Next 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