Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Statements in Visual Basic (http://www.go4expert.com/articles/statements-visual-basic-t3721/)

Sanskruti 2Apr2007 19:38

Statements in Visual Basic
 
A statement in Visual Basic is a complete instruction that can contain keywords, operators, variables, constants, and expressions. Following are the categories of statements
  • Declaration Statements,
  • Executable Statements,
  • Assignment Statements

Declaration Statements in Visual Basic



You use declaration statements to name and define procedures, variables, properties, arrays, and constants. When you declare a programming element, you can also define its data type, access level, and scope.

When the code containing a declaration statement runs, Visual Basic reserves the memory required for the declared element. If the element holds a value, Visual Basic initializes it to the default value for its data type. You can assign an initial value to a variable as part of its declaration, as the following example illustrates.

Code: VB

Dim a As Integer = 45
' Declaration creates a and assigns the value 45 to it.
 

If a variable is an object variable, you can explicitly create an instance of its class when you declare it by using the keyword as the following
Code: VB

Dim m As New System.Windows.Forms.Form()

Note that the initial value you specify in a declaration statement is not assigned to a variable until execution reaches its declaration statement. Until that time, the variable contains the default value for its data type.

Assignment Statements



Assignment statements carry out assignment operations, which consist of taking the value on the right side of the assignment operator (=) and storing it in the element on the left, as in the following example.
v = 42
In the example, the assignment statement stores the literal value 42 in the variable v.

Executable Statements



An executable statement performs an action. It can call a procedure, branch to another place in the code, loop through several statements, or evaluate an expression. An assignment statement is a special case of an executable statement. The following example uses an If...Then...Else control structure to run different blocks of code based on the value of a variable. Within each block of code, a For...Next loop runs a specified number of times.
Code: VB

Public Sub startWidget(ByVal aWidget As widget,ByVal clockwise As Boolean, ByVal r As Integer)
    Dim c As Integer
    If clockwise = True Then
        For c = 1 To r
            aWidget.spinClockwise()
        Next c
    Else
        For c = 1 To r
           aWidget.spinCounterClockwise()
        Next c
    End If
End Sub

The If statement in the example checks the value of the parameter clockwise. If the value is True, it calls the spinClockwise method of aWidget. If the value is False, it calls the spinCounterClockwise method of aWidget. The If...Then...Else control structure ends with End If.The For...Next loop within each block calls the appropriate method a number of times equal to the value of the r parameter.


All times are GMT +5.5. The time now is 01:49.