Showing posts with label VB.net Tutorial. Show all posts
Showing posts with label VB.net Tutorial. Show all posts

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 n 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 = 0 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 = 1 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 = 0 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 = 0 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 > 1000) Then
            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 I 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 I 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