Saturday 17 September 2011

MinValue and MaxValue of Long

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

Long = 1 Or 2

public class Test

   Public Const MASK_READ_WRITE As Long = Or 2
   public Shared Sub Main
        Console.WriteLine(MASK_READ_WRITE)
   End Sub
End class

Long = &H1000&

public class Test

   Public Const MASK_READ As Long = &H1000&
   public Shared Sub Main
        
   End Sub
End class

ULong of Oct

public class Test

   public Shared Sub Main
        Dim flags As ULong
        flags = &O144 ' Octal &O144 = 64 32 100.

        Console.WriteLine(Oct(flags)) ' Octal.
   End Sub

End class

reedlyULong of Hex

public class Test

   public Shared Sub Main
        Dim flags As ULong
        flags = &H64 ' Hexadecimal &H64 = 16 96 100.

        Console.WriteLine(Hex(flags)) ' Hexadecimal.
   End Sub

End class

ULong of Decimal

public class Test

   public Shared Sub Main
        Dim flags As ULong
        flags = 100 ' Decimal 100.

        Console.WriteLine(flags)      ' Decimal.
   End Sub

End class

MinValue and MaxValue of Short

public class Test
   public Shared Sub Main
               Dim sNum As Short

               Console.WriteLine("Short: " & sNum.MinValue & " to " & sNum.MaxValue)

   End Sub
End class

Short Overflow

Module Module1

    Sub Main()
        Dim Value As Short = 32767
        Value = Value + 1
    End Sub

End Module

Display negative SByte value using the standard numeric format specifiers and a number of specific CultureInfo objects

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultures() As CultureInfo = {CultureInfo.CreateSpecificCulture("en-US"), _
                                       CultureInfo.CreateSpecificCulture("fr-FR"), _
                                       CultureInfo.CreateSpecificCulture("es-ES") }
      Dim negativeNumber As SByte = -45
      Dim specifiers() As String = {"E2""F""N""P""X2"

      For Each specifier As String In specifiers
         For Each culture As CultureInfo In Cultures
            Console.WriteLine("{0,2} format using {1} culture: {2, 16} {3, 16}", _ 
                              specifier, culture.Name, _
                              negativeNumber.ToString(specifier, culture))

         Next
      Next
   End Sub
End Module

Byte

Convert the part of a Byte array to a String with the ToString method:
Example of some BitConverter.ToString( ) method overloads.
Imports System
Imports Microsoft.VisualBasic

Module BytesToStringDemo
    Sub Main( )
        Dim arrayOne as Byte( ) _
              0,   0,   0,   0128,  63,   0,   0112,  65, _
              0255127,  71,   0,   0128,  59,   0,   0, _
              0,   0,   0192255,   0,   0128255,   0, _
              0128127 }

        Console.WriteLine(BitConverter.ToStringarrayOne))
    End Sub 
End Modul

Explicit conversion of an integer to a string

Module Tester
    Public Sub Main()
        Dim iInteger As Integer = 5280
        Dim lLong As Long
        Dim bytByte As Byte
        Dim sngSingle As Single
        Dim dblDouble As Double
        Dim decDecimal As Decimal

        Console.WriteLine

("Explicit conversion of an integer to a string: {0}"
, CStr(iInteger))
    End Sub

End Module

Implicit conversion of an integer to a string

Module Tester
    Public Sub Main()
        Dim iInteger As Integer = 5280
        Dim lLong As Long
        Dim bytByte As Byte
        Dim sngSingle As Single
        Dim dblDouble As Double
        Dim decDecimal As Decimal

        Console.WriteLine("Implicit conversion of an integer to a string: {0}", iInteger)
    End Sub

End Module

Integer OverflowException

Public Class Tester
    Public Shared Sub Main
        Dim A, B As Integer
        Dim As Integer
        Try
            A = 9999
            B = 9999
            C = A * B * B * B
        Catch Except As OverflowException
            Console.WriteLine("Overflow error detected")
        End Try
    End Sub

End Class

Compare Integer value in If statement

Module Module1

    Sub Main()
        Dim TestScore As Integer = 80

        If TestScore >= 90 Then
            Console.WriteLine("Test grade: A")
        ElseIf TestScore >= 80 Then
            Console.WriteLine("Test grade: B")
        ElseIf TestScore >= 70 Then
            Console.WriteLine("Test grade: C")
        Else
            Console.WriteLine("Test grade: F")
        End If

    End Sub

End Module

Pass Integer to a function by value

Module Module1

    Sub NoChangeToParameter(ByVal A As Integer)
        A = 1001
        Console.WriteLine("Value of A in subroutine " & A)
    End Sub

    Sub Main()
        Dim Number As Integer = 100

        Console.WriteLine("Number before function call: " & Number)
        NoChangeToParameter(Number)
        Console.WriteLine("Number before function call: " & Number)
    End Sub

End Module

fanlPass Integer to a function by reference

Module Module1

    Sub ParameterChange(ByRef A As Integer)
        A = 1001
        Console.WriteLine("Value of A in subroutine " & A)
    End Sub

    Sub Main()
        Dim Number As Integer = 100

        Console.WriteLine("Number before function call: " & Number)
        ParameterChange(Number)
        Console.WriteLine("Number before function call: " & Number)
    End Sub

End Module

Friday 16 September 2011

Integer format: D10

Public Class Tester
    Public Shared Sub Main
        Dim intNumber As Integer = 12345
        Console.WriteLine(intNumber.ToString("D10"))

    End Sub
End Class

Parse Integer variable and get hash code from Integer

public class Test
   public Shared Sub Main
    Dim Number As String
    Number = "4"
    Console.WriteLine(Integer.Parse(Number))
    Console.WriteLine(Number.GetHashCode())

   End Sub
End class

Integer boolean calculation: Or, And, Xor, Not

public class Test
   public Shared Sub Main
        Dim As Integer
    
    
        I = Or 4
        Console.WriteLine(I)
    
        I = And 4
        Console.WriteLine(I)
    
        I = Xor 3
        Console.WriteLine(I)
    
        I = Not 5
        Console.WriteLine(I)
   End Sub
End class

Integer.GetType

Module Module1

  Const x As String = "This is a string"

  Sub Main()
    Dim As Double = 5.678
    Dim As Int32 = 123

    Console.WriteLine(a.ToString)
    Console.WriteLine(b.ToString)
    Console.WriteLine(456.987.ToString)

    Dim As Integer
    Console.WriteLine(c.GetType())
    Console.WriteLine(c.GetType().ToString)

  End Sub

End Module

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

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

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

Data Type

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

Option Explicit

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