|
 
 

Tutorial 2 - Basic VB Syntax and Semantics

Basic data types

 

1.Numeric

 

Data type

Size

Value range

Byte

1 byte

0 through 255 (unsigned).

Double
(double-precision floating-point)

8 bytes

-1.79769313 486231570E+308 through
-4.9406 56458412 46544E-324 for negative values; 4.940656458 41246544E-324 through 1.797 693134862 31570E+308 for positive values.

Integer

4 bytes

-2,147,483,648 through 2,147,483,647.

Long
(long integer)

8 bytes

-9,223,372,036,854,775, 808 through 9,223, 372, 036,854,775,807.

Currency

 8 bytes

store monetary values. It supports 4 digits to the right of decimal point and 15 digits to the left

Single
(single-precision floating-point)

4 bytes

-3.4028235E+38 through -1.401298E-45 for negative values; 1.40 1298E-45 through 3.4028235E+38 for positive values.

2. String

Use to store alphanumeric values. A variable length string can store approximately 4 billion characters

3. Date

Use to store date and time values. A variable declared as date type can store both date and time values and it can store date values 01/01/0100 up to 12/31/9999

4. Boolean

Boolean data types hold either a true or false value. These are not stored as numeric values and cannot be used as such. Values are internally stored as -1 (True) and 0 (False) and any non-zero value is considered as true.

5. Variant

Stores any type of data and is the default Visual Basic data type. In Visual Basic if we declare a variable without any data type by default the data type is assigned as default.

 

User defined data types

Structure

   In Visual Basic 6.0, you declare a structure using the Type ... End Type construction. The structure and its members all default to public access. Explicit access declaration is optional. The following example shows a valid structure declaration:

syntax :

 

Type  tag declarators

    [ Block

 End  Type

example :

               Type Employee

                      EmpNumber    As Integer

                      EmpOfficePhone  As String 

                      EmpHomePhone As String 

             End Type

 

 

Program execution control Statements

 

If...Then...Else Statements

 

    An If...Then...Else statement is the basic conditional statement. If the expression in the If statement is True, the statements enclosed by the If block are executed. If the expression is False, each of the ElseIf expressions is evaluated.

 

syntax :

 

If Expression [ Then ] StatementTerminator   

    [ Block

[ ElseIfStatement+ ]    

    [ Block ]

[ ElseStatement

     [ Block ]

End If StatementTerminator

example :

 

              Dim iNumber As Integer

            iNumber =  Val(Text1.Text)

           If iNumber < 5 Then

               Text2.Text= "Number is Less than Five"

            Else

                 Text2.Text=  "Number is Greater than Five"

            End If

       

Select Case

 

    A Select Case statement executes statements based on the value of an expression.

 

syntax :

 

Select [ Case ] Expression StatementTerminator    

    [ CaseStatement+ ]    

            ....

    [ CaseElseStatement ]    

End Select StatementTerminator

 

example :

   

               Dim x As Integer

                x = 10

            Select Case x

                Case 5

                    Text1.Text = "x = 5"

                Case 10

                    Text1.Text = "x = 10"

                Case 20

                    Text1.Text = "x = 20 ? 10"

                Case 30

                     Text1.Text = "x = 30"

            End Select

       

While Do While

 

    A While or Do loop statement loops based on a Boolean expression. A While loop statement loops as long as the Boolean expression evaluates to True;

 

syntax :

        1) While Expression StatementTerminator  

                  [ Block ]    

            Wend StatementTerminator

         2) Do [ WhileOrUntil Expression ] StatementTerminator    [ Block

                    Loop [ WhileOrUntil Expression ] StatementTerminator

 

example :

   

                 Dim x As Integer

            x = 3

            Do While x > 0

                 Text1.Text = "Do While x > 0"

                x = x - 1

            Loop

            x = 3

            While x < 5

                 Text1.Text = "While x < 5"

                x = x + 1

            Wend

            Do

                 Text1.Text = "While x = 2"

                x= x+ 1

            Loop While x > 2

       

For Loop

 

    For/Next loops enable you to evaluate a sequence of statements multiple times.

 

syntax :

 

For [Expression]

        [Block]

Next

 

example :

 

        Dim x As Integer

        For x = 1 To 10

             Text1.Text = "VB In Easy Steps"

        Next x

   


Main Features
» Easy to learn VB.

» Tutorials with source & video.

» Can master within 9 hours.

» All VB concepts in detail.

» Become confident in VB applications.

» Cost effective.
   
Subscribe Newsletter
Register to the site for free, and subscribe to the newsletter. You will receive new articles and special content not available elsewhere on the site, right into your e-mail box!    




 
Download VB In Easy Steps