|
 
 

Tutorial 2 - Basic VB .NET Syntax and Semantics
 

Basic data types

 

Data type CLR type structure Size Value range

Boolean

System.Boolean 2 bytes

True or False.

Byte

System.Byte

1 byte

0 through 255 (unsigned).

Char

System.Char

2 bytes

0 through 65535 (unsigned).

Date

System.DateTime

8 bytes

0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999.

Decimal

System.Decimal

16 bytes

0 through +/-79,228,162,514,264,337, 593,543,950,335 with no decimal point;
0 through +/-7.922816251426433 7593543950335 with 28 places to the right of the decimal; smallest nonzero number is
+/-0.0000000000000 000000000000001 (+/-1E-28).

Double
(double-precision floating-point)

System.Double

8 bytes

1.797693134862 31570E+308 through
-4.940656458412 46544E-324 for negative values; 4.940656458412 46544E-324 through 1.79769313486231570E+308 for positive values

Integer

System.Int32

4 bytes

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

Long
(long integer)

System.Int64

8 bytes

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

Object

System.Object (class)

4 bytes

Any type can be stored in a variable of type Object.

Short

System.Int16

2 bytes

-32,768 through 32,767.

Single
(single-precision floating-point)

System.Single

4 bytes

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

String
(variable-length)

System.String (class)

Depends on implementing platform

0 to approximately 2 billion Unicode characters.

User-Defined Type
(structure)

(inherits from System.ValueType)

Depends on implementing platform

Each member of the structure has a range determined by its data type and independent of the ranges of the other members.



example :


Module Module1

    Sub Main()

        Dim iCount AsInteger  'declaring an integer variable

        Dim cLetter AsChar  'declaring a character variable

        Dim strName AsString  'declaring a string variable

        iCount = 10

        cLetter = "L"

        strName = "Easy Steps"

        System.Console.WriteLine(iCount) 'printing into the console

        System.Console.WriteLine(cLetter)

        System.Console.WriteLine(strName)

    EndSub

EndModule


User defined data types


Class


    A class is a way to bind the data and its associated functions together. It allows the data and functions to be hidden if necessary, from external use. When defining a class we are creating a new abstract data type that can be treated like any other built in type.

 

syntax : 

 

     [Public | Protected | Friend | Protected Friend | Private] Class classname

         [Variable, Property, Method and Event Declarations]

      End Class        

 

example :

 

Module Module1

     PublicClass Person 'declaring the class

            Dim iAge As Integer

            Dim strName As String

            PublicSub SetDetails(ByVal age AsInteger, ByVal nameAs String)

                iAge = age

                strName = name

            EndSub

            PublicSub GetDetails()

                System.Console.WriteLine(iAge)

                System.Console.WriteLine(strName)

            EndSub

     EndClass

    Sub Main()

        Dim FirstPerson AsNew Person 'creating a class variable (object)

        FirstPerson.SetDetails(27, "Erick") 'calling a member function

        FirstPerson.GetDetails()

    EndSub

EndModule


Structure


    A structure associates one or more members with each other and with the structure itself. When you  declare a structure, it becomes a composite data type and you can declare variables of that type.


syntax :


    [Public | Protected | Friend | Protected Friend | Private] Structure structname
        {Dim | Public | Friend | Private} member1 As datatype1
        ...
        {Dim | Public | Friend | Private} memberN As datatypeN
    End Structure


example :


Module Module1

    PublicStructure Person 'declaring the structure

        Dim iAge AsInteger

        Dim strName As String

        PublicSub SetDetails(ByVal age AsInteger, ByVal name AsString)

            iAge = age

            strName = name

        EndSub

        PublicSub GetDetails()

            System.Console.WriteLine(iAge)

            System.Console.WriteLine(strName)

        EndSub

    EndStructure

    Sub Main()

        Dim FirstPerson As New Person 'creating a structure variable

        FirstPerson.SetDetails(27, "Erick") 'calling a memberfunction

        FirstPerson.GetDetails()

    EndSub

End Module


 

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 :

 

    Module Module1

        Sub Main()

            DimiNumber AsInteger

            iNumber = System.Console.ReadLine()

            IfiNumber < 5 Then

                System.Console.WriteLine("Number is Less than Five")

            Else

                System.Console.WriteLine("Number is Greater than Five")

            EndIf

        EndSub

    EndModule


 

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 :

   

    Module Module1

        Sub Main()

            Dim x As Integer = 10

            SelectCase x

                Case 5

                    Console.WriteLine("x = 5")

                Case 10

                    Console.WriteLine("x = 10")

                Case 20

                    Console.WriteLine("x = 20 – 10")

                Case 30

                    Console.WriteLine("x = 30")

            EndSelect

        EndSub

    EndModule

 


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 ]    

           End While StatementTerminator

         2) Do [ WhileOrUntil Expression ] StatementTerminator    [ Block

                    Loop [ WhileOrUntil Expression ] StatementTerminator

 

example :

   

     Module Module1

        Sub Main()

            Dim x As Integer

            x = 3

            DoWhile x > 0

                Console.WriteLine("Do While x > 0")

                x -= 1

            Loop

            x = 3

            While x < 5

                Console.WriteLine("While x < 5")

                x += 1

            EndWhile

            Do

                Console.WriteLine("While x = 2")

                x -= 1

            LoopWhile x > 2

        EndSub

    End Module

 


For Loop


 

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

 

syntax :

 

For [Expression]

        [Block]

Next

 

example :

 

Module Module1

    Sub Main()

        Dim x As Integer

        For x = 1 To 10

            System.Console.WriteLine("VB Dot Net in Easy Steps")

        Next x

    EndSub

End Module

 


For Each Loop


    A For Each...Next statement loops based on the elements in an expression. A For Each statement specifies a loop control variable and an enumerator expression.

 

syntax :

 

    For Each LoopControlVariable In Expression StatementTerminator   

         [ Block ]  

    Next [Expression ] StatementTerminator

 

example :

 

Module Module1

    Sub Main()

        Dim x(9) As Integer

        Dim i As Integer="2"> = 0

        For i = 0 To 9

            x(i) = i + 1

        Next

        ForEach y AsInteger In x

            System.Console.WriteLine(y)

        Next

    EndSub

End Module



   





 
Download VB.NET In Easy Steps