Saturday, September 17, 2011

Parse Integer

public class Test
   public Shared Sub Main
        Try
            Dim num_items As Integer = Integer.Parse("123")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
   End Sub
End class

MinValue and MaxValue of Integer

public class Test
   public Shared Sub Main
               Dim iNum As Integer
               Console.WriteLine("Integer: " & iNum.MinValue & " to " & iNum.MaxValue)
   End Sub
End class

Swap two integers without using a third

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Public Class Tester
    Public Shared Sub Main
        
        Dim firstValue As Integer
        Dim secondValue As Integer

        firstValue = 17
        secondValue = 123
        Console.WriteLine("Before swap: {0}, {1}",firstValue, secondValue)

        firstValue = firstValue Xor secondValue
        secondValue = firstValue Xor secondValue
        firstValue = firstValue Xor secondValue
        Console.WriteLine("After swap: {0}, {1}",firstValue, secondValue)
    End Sub
End Class

Integer calculation

public class Test
   public Shared Sub Main
        Dim As Integer

        ' try adding numbers...
        n = 16
        n += 10.23
        Console.WriteLine("Addition " & n)

        ' try subtracting numbers...
        n = 24
        n -= 2
        Console.WriteLine("Subtraction " & n)

        ' try multiplying numbers...
        n = 6
        n *= 10
        Console.WriteLine("Multiplication " & n)

        ' try dividing numbers...
        n = 12
        n /= 6
        Console.WriteLine("Division " & n)


   End Sub
End class

Add two integers together

Module Tester

   Sub Main()
      Dim firstNumber, secondNumber As String

      Dim number1, number2, sumOfNumbers As Integer

      firstNumber = 10

      secondNumber = 20

      number1 = firstNumber
      number2 = secondNumber

      sumOfNumbers = number1 + number2 ' add numbers
      Console.WriteLine("The sum is {0}", sumOfNumbers)

   End Sub ' Main

End Module

Define Integer variable and assign value

Module Module1
    Sub Main( )
       Dim myInt As Integer = 7
       Console.WriteLine("Initialized myInt: {0}", myInt)
       myInt = 5
       Console.WriteLine("After assignment myInt: {0}", myInt)
    End Sub
 End Module

Integer Family MaxValue

Public Class Tester
    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder()
        result.AppendLine("MaxValue...")

        Dim maxByte As Byte = Byte.MaxValue
        Dim maxSByte As SByte = SByte.MaxValue
        Dim maxShort As Short = Short.MaxValue
        Dim maxUShort As UShort = UShort.MaxValue
        Dim maxInteger As Integer = Integer.MaxValue
        Dim maxUInteger As UInteger = UInteger.MaxValue
        Dim maxLong As Long = Long.MaxValue
        Dim maxULong As ULong = ULong.MaxValue

        result.Append("Byte ").AppendLine(maxByte)
        result.Append("SByte ").AppendLine(maxSByte)
        result.Append("Short ").AppendLine(maxShort)
        result.Append("UShort = ").AppendLine(maxUShort)
        result.Append("Integer = ").AppendLine(maxInteger)
        result.Append("UInteger = ").AppendLine(maxUInteger)
        result.Append("Long = ").AppendLine(maxLong)
        result.Append("ULong = ").AppendLine(maxULong)

        Console.WriteLine(result.ToString())
     End Sub
End Class

Hexadecimal Byte, UInteger and Integer

Option Strict On

Public Module modMain
   Public Sub Main()
      Dim maxValue As Byte = &HFF
      Dim posValue As UInteger = &HF034
      Dim negValue As Integer = &HF034
   
      Console.WriteLine(maxValue)
      Console.WriteLine(posValue)
      Console.WriteLine(negValue)
   End Sub
End Module

Turn Explicit off to use variable without declaration

Option Explicit Off

Module Module1

    Sub Main()
        EmployeeName = "Buddy Jamsa"
        EmployeePhoneNumber = "555-1212"
        EmployeeSalary = 45000.0
        NumberOfEmployees = 1

        Console.WriteLine("Number of employees: " & NumberOfEmployees)
        Console.WriteLine("Employee name: " & EmployeName)
        Console.WriteLine("Employee phone number: " & EmployeePhoneNumber)
        Console.WriteLine("Employee salary: " & EmployeeSalary)

    End Sub

End Module

Option Explicit Off

Option Explicit Off

Module Explicit
   Public Sub Main()
      For ctr As Integer = to 100
         ' Do something
         result = cntr
      Next
      Console.WriteLine("The counter reached " & result & ".")
   End Sub
End Module

Cause compiler error when Option Strict On

Module Module1
    
  Sub Main()

    Dim AnInt As Integer = 5
    Dim ALong As Long = 7

    ALong = AnInt
    'causes compiler error when Option Strict On
    'AnInt = ALong
    MsgBox(AnInt)

  End Sub

End Module

Call static method

Option Strict On

Public Module CallStaticMethod
   Public Sub Main()
      Console.WriteLine(Greeting.SayHello())
   End Sub
End Module

Public Class Greeting
   Public Shared Function SayHello() As String
      Return "And a top of the morning to you!"
   End Function
End Class

static Variable

Option Strict On

Public Module Test
   Public Sub Main()
      For loopCtr As Integer = to 10
         Console.WriteLine(Invocations())
      Next
   End Sub

   Private Function Invocations() As Integer
      Static i As Integer
      i += 1
      Return i
   End Function
End Module

Use namespace to remove the conflicts

Namespace Network
    Class Address
        Public IP As String
        Public DomainName As String

        Public Sub New(ByVal IPAddr As String, ByVal Domain As String)
            IP = IPAddr
            DomainName = Domain
        End Sub

        Public Sub ShowAddress()
            Console.WriteLine("IP: " & IP)
            Console.WriteLine("Domain: " & DomainName)
        End Sub
    End Class

End Namespace

Namespace Mailing
    Class Address
        Public Street As String
        Public City As String
        Public State As String
        Public Zip As String

        Public Sub New(ByVal Street As String, ByVal City As String,

 ByVal State As String, ByVal Zip As String)
            Me.Street = Street
            Me.City = City
            Me.State = State
            Me.Zip = Zip
        End Sub

        Public Sub ShowAddress()
            Console.WriteLine("Street: " & Street)
            Console.WriteLine("City: " & City)
            Console.WriteLine("State: " & State)
            Console.WriteLine("Zip: " & Zip)
        End Sub
    End Class

End Namespace

Module Module1
    Sub Main()
        Dim IP As New Network.Address("122.111.222.112""www.SomeSite.com")
        Dim address As New Mailing.Address("122 Main", _
          "Houston""Texas""77469")

        IP.ShowAddress()
        Console.WriteLine()
        address.ShowAddress()
    End Sub

End Module

Use Namespace

Namespace MyApp.Info
    Module Main
        Sub Main()
            Dim objHW As New MyApp.Info.Utilities
            objHW.DisplayData()
        End Sub
    End Module

    Public Class Utilities
        'Run the application
        Public Sub DisplayData()
            Console.WriteLine(Environment.MachineName)
            Console.WriteLine(Environment.SystemDirectory)
            Console.WriteLine(Environment.GetLogicalDrives())
            Console.WriteLine(Environment.Version.ToString())
        End Sub
    End Class
End Namespace

Define your own namespace

Namespace MyNamespace
    Public Class Class2

    End Class
End Namespace

Namespace MyNamespace
    Public Class Class1

    End Class
End Namespace


Module mod1
    Sub main()
        Dim objClass2 As MyNamespace.Class2
        Dim objClass1 As MyNamespace.Class1
    End Sub
End Module

class definition with namespace

namespace WinForms
       public class HelloWorld
               shared sub Main() 
                       System.Console.WriteLine("Hello World")
               end sub
       end class
end namespace

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

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

Explain the different parts that constitute ASP.NET application.

Content, program logic and configuration file constitute an ASP.NET application. Content files Content files include static text, images ...