Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Introduction to Visual Basic

Arrays

Volgend Onderwerp

Numeric Literals and Type Coercion

Vorig Onderwerp

Floating Point Notation - Single Double Decimal

|

Char and String Datatypes

Volgend Onderwerp
Numeric Literals without Type Coercion

Numeric Literals without Type Coercion

Numeric Literals with Type Coercion

Numeric Literals with Type Coercion

Hexadecimal and Octal Literals

Hexadecimal and Octal Literals



Numeric Literals without Type Coercion


Each literal expression ( without type coercion ) formed by an integral numeric value, will be regarded ( by the compiler ) as an Integer or Long expression.

When the represented value falls out of the Integer range, it will be regarded as an Long expression ( at least if it fits in the range of Long ).

Each literal expression ( without type coercion ) formed by an numeric decimal ( with digits after the decimal separator ), will be regarded ( by the compiler ) as an Double expression ( at least if it fits in the range of Double ).


Module Example1
    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
Download Broncode

Output :

 Integer :
 Min Value : -2147483648
 Max Value : 2147483647
 Max Literal Value : 2147483647

 Long :
 Min Value : -9223372036854775808
 Max Value : 9223372036854775807
 Max Literal Value : 9223372036854775807

 Double :
 Negative Min ( Literal ) Value : -1,79769313486232E+308
 Negative Max ( Literal ) Value : -4,94065645841247E-324
 Zero ( Literal ) : 0
 Positive Min ( Literal ) Value : 4,94065645841247E-324
 Positive Max ( Literal ) Value : 1,79769313486232E+308

An expression -2147483648 that tries to represent the minimum Integer value will be regarded ( by the compiler ) as a Long expression.
This expression is composed of the unary negation operator '-' and numeric literal '2147483648'. The parser read the numeric part '2147483648', and tries to imply the unary negation operator '-'. But the numeric part '2147483648' doesn't fit the 'Integer' range, an therefore will be regarded as a 'Long' expression.

Expression '-9223372036854775808' will ( for the same reason ) be refused by the compiler because '9223372036854775808' doesn't fit in the range of 'Long'. This expression leads to an "overflow" error.


Klik hier om terug naar boven te gaan.  Up


Numeric Literals with Type Coercion


Type coercion allows to form numeric literals of other datatypes then Integer, Long or Double.

By using a type character as suffix one could define the datatype of a numeric literal.


 Datatype :        Type character :

 Byte              <none>
 SByte             <none>
 Short             S
 UShort            US
 Integer           I
 UInteger          UI
 Long              L
 ULong             UL

 Single            F ( "Float" )
 Double            R ( "Real" )
 Decimal           D

Module Example2
    Sub Main()
        Dim value As Decimal = 9223372036854775808D                       ' (1)
    End Sub
End Module
Download Broncode

Klik hier om terug naar boven te gaan.  Up


Hexadecimal and Octal Literals


Usually integral literals are formed using the decimal scale ( base 10 ).
Using a hexadecimal ( base 16 ) or octal ( base 8 ) scale is possible, but then use prefixes &H and &O.


Module Example3
    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
Download Broncode

Output :

 15
 16
 7
 8

Updated On : 2008-10-25

Download Broncode

Published On : 2008-11-06

Numeric Literals and Type Coercion

Vorig Onderwerp

Floating Point Notation - Single Double Decimal

|

Char and String Datatypes

Volgend Onderwerp

Introduction to Visual Basic

Arrays

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.