Statements in Visual Basic

Discussion in 'Visual Basic [VB]' started by Sanskruti, Apr 2, 2007.

  1. Sanskruti

    Sanskruti New Member

    Jan 7, 2007
    Likes Received:
    Trophy Points:
    Software Consultant
    Mumbai, India
    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.

    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
    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.
    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 
    		Next c 
    		For c = 1 To r
    		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.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice