Monday 29 August 2011

Module global variable

Module Module1

    Sub Main()
        For intLoopIndex As Integer = To 5
            System.Console.WriteLine(Tracker())
        Next intLoopIndex
    End Sub

    Dim intCount As Integer

    Function Tracker() As Integer
        intCount += 1
        Return intCount
    End Function

End Module

Local variable shadows global variable with the same name

Module Module1

    Dim Counter As Integer

    Sub BigLoop()
        For Counter = 1000 To 1005      ' Use global Counter
            Console.Write(Counter & " ")
        Next
    End Sub

    Sub LittleLoop()
        Dim Counter As Integer

        For Counter = To 5      ' Use local Counter
            Console.Write(Counter & " ")
        Next
    End Sub

    Sub Main()
        Counter = 100

        Console.WriteLine("Starting Counter: " & Counter)
        BigLoop()
        Console.WriteLine("Counter after BigLoop: " & Counter)
        LittleLoop()
        Console.WriteLine("Counter after LittleLoop: " & Counter)

        If (Counter > 1000Then
            Dim Counter As Integer = 0

            Console.WriteLine("Counter in If statement: " & Counter)
        End If

        Console.WriteLine("Ending Counter: " & Counter)
    End Sub

End Module

Function local variables

Module Module1

    Sub F()
        Dim Name As String = "www.java2s.com"
        Dim Price As Double = 17.45
        Dim As Integer = 1001

        Console.WriteLine("In F")
        Console.WriteLine("Name: " & Name)
        Console.WriteLine("Price: " & Price)
        Console.WriteLine("I: " & I)
    End Sub

    Sub FF()
        Dim Name As String = "string"
        Dim Price As Double = 49.99
        Dim As Integer = 0

        Console.WriteLine("In FF")
        Console.WriteLine("Name: " & Name)
        Console.WriteLine("Price: " & Price)
        Console.WriteLine("I: " & I)
    End Sub

    Sub Main()
        F()
        Console.WriteLine()
        FF()
    End Sub

End Module

Sub scope

public class Test
   public Shared Sub Main
        DisplayHowardsName()
        DisplayStephsName()
   End Sub
   Shared Sub DisplayStephsName()
        Dim myName As String
        myName = "A"

        Console.WriteLine(myName)

    End Sub

   Shared Sub DisplayHowardsName()
        Dim myName As String
        myName = "B"

        Console.WriteLine(myName)
    End Sub
  
End class

Define variable inside If statement

public class Test
   public Shared Sub Main

        Dim manager As Boolean = True
        If manager Then Dim txt As String = "M" : Console.WriteLine(txtElse _
            Dim txt As String = "E" : Console.WriteLine(txt)

   End Sub
End class

Variable scope in try catch statement

public class Test
   public Shared Sub Main


        Try
            Dim As Integer = CInt("bad value")
        Catch ex As InvalidCastException
            Dim txt As String = "InvalidCastException"
            Console.WriteLine(txt)
        Catch ex As Exception
            Dim txt As String = "Exception"
            Console.WriteLine(txt)
        End Try

   End Sub
End class

Variable block scope

public class Test
   public Shared Sub Main
        For i As Integer = To 5
            Dim As Integer = 3
            If i = j Then
                Dim As Integer = i + j
                Console.WriteLine("M: " & M)
            Else
                Dim As Integer = i * j
                Console.WriteLine("N: " & N)
            End If
            Dim As Integer = 123
            Console.WriteLine("k: " & k)
        Next i
   End Sub
End class

Block scope

Option Strict On

Public Class BlockScope
   Public Shared Sub Main()
   
      For outerLoop As Integer = to 10000
         For innerLoop As Integer = to 10
            Dim blockVar As Integer
            blockVar += 1
            If blockVar Mod 1000 Then 
                Console.WriteLine(blockVar)
            End If
         Next
      Next
   
   End Sub
End Class

Demonstrates scope rules and instance variables

Public Class Tester
   ' instance variable can be used anywhere in class
   Dim Shared value As Integer = 1

   ' demonstrates class scope and block scope
   Public Shared Sub Main
      Dim value As Integer = 5

      Console.WriteLine("local variable value in" & _
         " FrmScoping_Load is " & value )

      MethodA() ' MethodA has automatic local value
      MethodB() ' MethodB uses instance variable value
      MethodA() ' MethodA creates new automatic local value
      MethodB() ' instance variable value retains its value

      Console.WriteLine("local variable " & _
         "value in FrmScoping_Load is " & value )
   End Sub 

   ' automatic local variable value hides instance variable
   Shared Sub  MethodA()
      Dim value As Integer = 25 ' initialized after each call

      Console.WriteLine("local variable " & _
         "value in MethodA is " & value & " after entering MethodA" )
      value += 1
      Console.WriteLine("local variable " & _
         "value in MethodA is " & value & " before exiting MethodA" )
   End Sub 

   ' uses instance variable value
   Shared Sub  MethodB()
      Console.WriteLine("instance variable" & _
         " value is " & value & " after entering MethodB" )
      value *= 10
      Console.WriteLine("instance variable " & _
         "value is " & value & " before exiting MethodB" )
   End Sub

End Class

Use Console.WriteLine to display various type variables

Module Module1

    Sub Main()
        Dim As Integer = 100
        Dim As Double = 0.123456789
        Dim Message As String = "Hello, VB World!"

        Console.WriteLine(A)
        Console.WriteLine("The value of A is " & A)
        Console.WriteLine(B)
        Console.WriteLine(B & " plus " & A & " = " & B + A)
        Console.WriteLine(Message)

    End Sub

End Module

Reference variable index in Console.WriteLine

Module Module1

    Sub Main()
        Dim As Double = 1.23456789
        Console.WriteLine("{0} {1} {2}"123)

    End Sub

End Module

Concatenate strings in Console.WriteLine statement

Module Module1

    Sub Main()
        Dim WebSite As String
        Dim Publisher As String = "AAAA"

        Console.WriteLine("AAAA: " & Publisher)

        WebSite = "CCCC." & Publisher & ".DDDD"
        Console.WriteLine("String one " & WebSite)

    End Sub

End Module

Write some text based on the specified data type

Module Module1

    Sub Main()

        Console.Write(True)
        Console.Write(25)
        Console.Write("Some text.")
        Console.WriteLine()
        Console.WriteLine()
        Console.WriteLine(True)
        Console.WriteLine(25)
        Console.WriteLine("Some text.")
    End Sub

End Modul

Variable index

Public Class Tester
    Public Shared Sub Main

        Console.WriteLine(String.Format_
            "There are about {0} days in {1} years.", _
            365.25 3317))
 
    End Sub

End Class

Output string to the Console

Module Module1

     Sub Main( )
       Console.WriteLine("Hello from Module")
     End Sub

 End Module

Match a pattern from user input

Public Class PatternMatcher
    Shared Sub Main()
        Dim sInput As String
        Dim sPattern As String
        Dim sMatch As String

        System.Console.Write("Please Enter A Pattern:")
        sInput = System.Console.ReadLine()
        sPattern = sInput

        System.Console.Write("Please Enter A String To Compare Against:")
        sInput = System.Console.ReadLine()
        sMatch = sInput

        If sMatch Like sPattern Then
            System.Console.WriteLine(sMatch & " Matched with " & sPattern)
        Else
            System.Console.WriteLine(sMatch & " did not Match with " & sPattern)
        End If
    End Sub
End Class

Use While to read user input

Sub Main()
        Console.WriteLine("Please enter 'q' to quit...")
        Dim strInput As String = Console.ReadLine()

        While (strInput <> "q")
            Console.WriteLine("You typed " & strInput)
            Console.WriteLine("Please enter 'q' to quit...")
            strInput = Console.ReadLine()
        End While
        Console.WriteLine("Quitting now.")

    End Sub

Use While to read user input

Sub Main()
        Console.WriteLine("Please enter 'q' to quit...")
        Dim strInput As String = Console.ReadLine()

        While (strInput <> "q")
            Console.WriteLine("You typed " & strInput)
            Console.WriteLine("Please enter 'q' to quit...")
            strInput = Console.ReadLine()
        End While
        Console.WriteLine("Quitting now.")

    End Sub

Convert what you type to value type

Module Module1

    Sub Main()
        Dim intInput As Integer
        Console.WriteLine("Enter a temperature...")
        intInput = Val(Console.ReadLine())
        If intInput > 75 Then
            Console.WriteLine("Too hot!")
        ElseIf intInput < 55 Then
            Console.WriteLine("Too cold!")
        Else
            Console.WriteLine("Just right!")
        End If
    End Sub

Convert what you type to value type

Module Module1

    Sub Main()
        Dim intInput As Integer
        Console.WriteLine("Enter a temperature...")
        intInput = Val(Console.ReadLine())
        If intInput > 75 Then
            Console.WriteLine("Too hot!")
        ElseIf intInput < 55 Then
            Console.WriteLine("Too cold!")
        Else
            Console.WriteLine("Just right!")
        End If
    End Sub

Use Do Loop to read user input

Module Module1

    Sub Main()
        Dim strInput As String

        Do
            Console.WriteLine("Please enter 'q' to quit...")
            strInput = Console.ReadLine()
            Console.WriteLine("You typed " & strInput)
        Loop While (strInput <> "q")
        Console.WriteLine("Quitting now.")
    End Sub

End Module

Read keyboard input

Class Tester
     Shared Sub Main()
        Dim userInput As String

            Console.WriteLine("Enter a source temperature.")
            userInput = Console.ReadLine()
            Console.WriteLine(userInput)
     End Sub

End Class

Read a string from console

  Sub Main()
        Dim strMessage As String
        Try
            strMessage = Console.ReadLine()
            Console.WriteLine("HELLO " + strMessage)
        Catch ex As Exception
        End Try
    End Sub

Read a complete line of text

Sub Main()

        Dim strLine As String
        Console.Write("Enter a line of text: ")
        strLine = Console.ReadLine
        Console.WriteLine()
        Console.WriteLine("You just entered: {0}", strLine)
    End Sub

Read a single character

Sub Main()

        Dim strChar As String
        Console.Write("Enter a character: ")
        strChar = Console.ReadKey.KeyChar
        'strChar = Console.ReadKey(True).KeyChar
        Console.WriteLine()
        Console.WriteLine("You just entered {0}.", strChar)


    End Sub

Every console app starts with Main

Module HelloWorld
   Sub Main( )
     System.Console.WriteLine("Hello world!")
   End Sub
End Module

Friday 26 August 2011

Introduction

1.1.1.Compile Visual Basic source code in command line


vbc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.vb

1.1.2.How to compile and run the code in this tutorial

Go to: Start->Programs->Visual Studio.NET->Visual Studio.NET Tools-> Visual Studio.NET Command Prompt, and type:
c:\examples>vbc example1.vb.

1.1.3.Simple Visual Basic program.

Module Tester

   Sub Main()
      Console.WriteLine("Welcome to Visual Basic!")
   End Sub ' Main

End Module
 
 
 1.1.4.Writing line of text with multiple statements.

Module Tester

   Sub Main()
      Console.Write("Welcome to ")
      Console.WriteLine("Visual Basic!")
   End Sub ' Main

End Module