' Visual Basic 2008 9.0 .NET Examples - Datatypes - Introduction to Visual Basic : Module Example1 Sub Main() Dim someDouble As Double = 1.5 ' (1) Console.WriteLine(someDouble) ' Console.ReadLine() End Sub End Module Module Example2 Sub Main() Console.WriteLine("Integer :") Console.WriteLine("Min Value : " & Integer.MinValue) Console.WriteLine("Max Value : " & Integer.MaxValue) Console.WriteLine("Max Literal Value : " & 2147483647) Console.WriteLine() ' Console.WriteLine("Long :") Console.WriteLine("Min Value : " & Long.MinValue) Console.WriteLine("Max Value : " & Long.MaxValue) Console.WriteLine("Max Literal Value : " & 9223372036854775807) Console.WriteLine() ' Console.WriteLine("Double :") Console.WriteLine("Negative Min ( Literal ) Value : " & _ -1.7976931348623157E+308) ' Double.MinValue Console.WriteLine("Negative Max ( Literal ) Value : " & _ -4.94065645841247E-324) Console.WriteLine("Zero ( Literal ) : " & 0.0) Console.WriteLine("Positive Min ( Literal ) Value : " & _ +4.94065645841247E-324) ' Double.Epsilon Console.WriteLine("Positive Max ( Literal ) Value : " & _ +1.7976931348623157E+308) ' Double.MaxValue ' Console.ReadLine() End Sub End Module Module Example3 Sub Main() Dim value As Decimal = 9223372036854775808D ' (1) End Sub End Module Module Example4 Sub Main() Dim value1 As Short = &HF Console.WriteLine(value1) ' Dim value2 As Short = &H10 Console.WriteLine(value2) ' Dim value3 As Short = &O7 Console.WriteLine(value3) ' Dim value4 As Short = &O10 Console.WriteLine(value4) ' Console.ReadLine() End Sub End Module Module Example5 Sub Main() Dim units As Single() = _ {500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01} ' Dim amount As Single = 0.06 Console.Write(amount & " : ") ' Dim index As Integer Do While amount > 0 Do While amount - units(index) >= 0 Console.Write(units(index) & " ") amount -= units(index) Loop index += 1 Loop Console.WriteLine() ' Console.ReadLine() End Sub End Module Module Example6 Sub Main() Console.WriteLine(2.0 Mod 0.2 = 0) Console.WriteLine(2.0 Mod 0.2) ' Dim someSingle As Single = 4.99 Console.WriteLine(someSingle * 17 = 84.83) Console.WriteLine(someSingle * 17) ' someSingle = 1 / 107.0 Console.WriteLine(someSingle * 107 = 1) Console.WriteLine(someSingle * 107) ' Console.ReadLine() End Sub End Module Module Example7 Public Sub Main() Console.WriteLine("seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm") Console.WriteLine(GetBinary(1.17549435E-38F) & " : " & _ 1.17549435E-38F.ToString()) Console.WriteLine(GetBinary(0.5F) & " : " & 0.5F.ToString()) Console.WriteLine(GetBinary(0.6F) & " : " & 0.6F.ToString()) Console.WriteLine(GetBinary(Single.MaxValue) & " : " & _ Single.MaxValue.ToString()) Console.WriteLine(GetBinary(-1.17549435E-38F) & " : " & _ -1.17549435E-38F.ToString()) Console.WriteLine(GetBinary(Single.MinValue) & " : " & _ Single.MinValue.ToString()) Console.WriteLine(GetBinary(0.0F) & " : " & 0.0F.ToString()) Console.WriteLine(GetBinary(-0.0F) & " : " & -0.0F.ToString()) Console.WriteLine(GetBinary(Single.Epsilon) & " : " & _ Single.Epsilon.ToString()) Console.WriteLine(GetBinary(-1.401298E-45F) & " : " & _ -1.401298E-45F.ToString()) Console.WriteLine(GetBinary(Single.PositiveInfinity) & " : " & _ Single.PositiveInfinity.ToString()) Console.WriteLine(GetBinary(Single.NegativeInfinity) & " : " & _ Single.NegativeInfinity.ToString()) ' Console.ReadLine() End Sub Public Function GetBinary(ByVal value As Byte) As String For counter As Integer = 1 To 8 GetBinary = (value Mod 2).ToString() & GetBinary value >>= 1 Next End Function Public Function GetBinary(ByVal value As Single) As String If BitConverter.IsLittleEndian Then For Each byteElement As Byte In BitConverter.GetBytes(value) GetBinary = GetBinary(byteElement) & GetBinary Next Else Throw New ApplicationException("Only Little Endian supported.") End If End Function End Module Module Example8 Sub Main() Dim singleOperands As Single() = {Single.PositiveInfinity, _ Single.NegativeInfinity, _ 123.0F, -123.0F, 0.0F, -0.0F} Dim operatorSymbols As String() = {"*", "/", "+", "-"} ' For Each operatorSymbol As String In operatorSymbols Console.WriteLine("OPERATOR " & operatorSymbol.ToString()) Console.WriteLine() For Each singleOperand1 As Single In singleOperands For Each singleOperand2 As Single In singleOperands PrintCalculation(singleOperand1, operatorSymbol, _ singleOperand2) Next Console.WriteLine() Next Console.WriteLine() Next ' Console.ReadLine() End Sub Sub PrintCalculation(ByVal operand1 As Single, _ ByVal operatorSymbol As String, _ ByVal operand2 As Single) Console.Write(GetString(operand1) & " " & operatorSymbol & " " & _ GetString(operand2) & " = ") Select Case operatorSymbol Case "*" Console.WriteLine(GetString(operand1 * operand2)) Case "/" Console.WriteLine(GetString(operand1 / operand2)) Case "+" Console.WriteLine(GetString(operand1 + operand2)) Case "-" Console.WriteLine(GetString(operand1 - operand2)) End Select End Sub Function GetString(ByVal value As Single) As String If IsPositiveZero(value) Then GetString = "+0" ElseIf IsNegativeZero(value) Then GetString = "-0" ElseIf Single.IsNegativeInfinity(value) Then GetString = "-Infinity" ElseIf Single.IsPositiveInfinity(value) Then GetString = "+Infinity" ElseIf Single.IsNaN(value) Then GetString = "NaN" Else GetString = value.ToString() End If End Function Public Function IsPositiveZero(ByVal value As Single) As Boolean If BitConverter.GetBytes(value)(0) = 0 AndAlso _ BitConverter.GetBytes(value)(1) = 0 AndAlso _ BitConverter.GetBytes(value)(2) = 0 AndAlso _ BitConverter.GetBytes(value)(3) = 0 Then _ IsPositiveZero = True End Function Public Function IsNegativeZero(ByVal value As Single) As Boolean If BitConverter.GetBytes(value)(0) = 0 AndAlso _ BitConverter.GetBytes(value)(1) = 0 AndAlso _ BitConverter.GetBytes(value)(2) = 0 AndAlso _ BitConverter.GetBytes(value)(3) = 128 Then _ IsNegativeZero = True End Function End Module Module Example9 Sub Main() Dim someCharacter As Char Console.WriteLine("*" & someCharacter & "*") ' (1) ' someCharacter = "a"c ' (2) Console.WriteLine("*" & someCharacter & "*") ' Console.ReadLine() End Sub End Module Module Example10 Sub Main() ' Default & Minimum Value : ' January 1, 0001, 12:00:00 AM ( midnight ) : Dim someDate As Date Console.WriteLine(someDate) ' ' Maximum Literal Value : ' December 31, 9999, 11:59:59 PM ( 1 second before midnight ) : someDate = #12/31/9999 11:59:59 PM# Console.WriteLine(someDate) ' ' Minimum ( Literal ) Value : ' January 1, 0001, 12:00:00 AM ( midnight ) : someDate = #12:00:00 AM# Console.WriteLine(someDate) ' ' October 17, 2000, 12:00:00 AM ( midnight ) : someDate = #10/17/2007# Console.WriteLine(someDate) ' ' October 17, 2000, 12:00:01 AM ( 1 seconde after midnight ) : someDate = #10/17/2007 12:00:01 AM# Console.WriteLine(someDate) ' ' October 17, 2007, 1:00:00 AM ( 1 o'clock in the morning ) : someDate = #10/17/2007 1:00:00 AM# Console.WriteLine(someDate) ' ' October 17, 2007, 11:59:59 AM ( 1 second before noon ) : someDate = #10/17/2007 11:59:59 AM# Console.WriteLine(someDate) ' ' October 17, 2007, 12:00:00 PM ( noon ) : someDate = #10/17/2007 12:00:00 PM# Console.WriteLine(someDate) ' ' October 17, 2007, 12:00:01 PM ( 1 second after noon ) : someDate = #10/17/2007 12:00:01 PM# Console.WriteLine(someDate) ' ' October 17, 2007, 1:00:00 PM ( 1 o'clock in the afternoon ) : someDate = #10/17/2007 1:00:00 PM# Console.WriteLine(someDate) ' ' October 17, 2007, 11:59:59 PM ( 1 second before midnight ) : someDate = #10/17/2007 11:59:59 PM# Console.WriteLine(someDate) ' ' October 18, 2007, 12:00:00 AM ( midnight ) : someDate = #10/18/2007# Console.WriteLine(someDate) ' Console.ReadLine() End Sub End Module ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.