Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 parser incorrectly evaluates the data type if the minimum value of the numeric data types is parsed (811402)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SYMPTOMS

If the minimum value of the numeric data types is parsed, the Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 parser incorrectly evaluates the data type. For example, if you use an Integer data type, the Visual Basic .NET or Visual Basic 2005 parser incorrectly evaluates -(2^31) as Long rather than Integer. However, the Microsoft Visual C# .NET or Microsoft Visual C# 2005 parser appropriately evaluates the data type.

CAUSE

This behavior occurs because both Visual Basic .NET (or Visual Basic 2005) and Visual C# have syntactic grammar that does not include the leading hyphen (-) as part of the numeric literal. Therefore, -(2^31) is parsed as 2^31, and 2^31 is Long.

Visual C# .NET or Visual C# 2005 has a special rule to handle the hyphen prefix for numbers. Visual Basic .NET or Visual Basic 2005 does not contain any special handling to make its behavior consistent with Microsoft Visual Basic 6.0.

WORKAROUND

To work around this behavior, complete one of the following steps:
  • Use the MinValue property of the numeric data type. For example, use Integer.MinValue.
  • Use numeric expression that evaluates to the minimum value. For example, rather than -2147483648, use -2147483647 -1.

STATUS

This behavior is by design.

MORE INFORMATION

The other numeric data types (such as Short and Long) operate the same way.

Steps to Reproduce the Behavior

  1. Create a new Visual Basic .NET or Visual Basic 2005 Console Application project.

    By default, Module1.vb is created.
  2. Add the following code to the Sub Main() method of Module1.vb:
          ' By using an Expression that results to Minimum Value of Integer data type
    Console.WriteLine("1) Hex(-2147483647 - 1) = {0}, Data type is {1}", Hex(-2147483647 - 1), (-2147483647 - 1).GetType().ToString())
          
    ' By directly using the Minimum Value of Integer data type
    Console.WriteLine("2) Hex(-2147483648) = {0}, Data type is {1}", Hex(-2147483648), (-2147483648).GetType().ToString())
    
    ' By using MinValue method of Integer data type to get Minimum Value
    Console.WriteLine("3) Hex(Integer.MinValue) = {0}, Data type is {1}", Hex(Integer.MinValue), (Integer.MinValue).GetType().ToString())
    
    Console.Read()
    
  3. On the Debug menu, click Start to run the sample code.
  4. The following results are displayed on the Console window:
    1) Hex(-2147483647 - 1) = 80000000, Data type is System.Int32
    2) Hex(-2147483648) = FFFFFFFF80000000, Data type is System.Int64
    3) Hex(Integer.MinValue) = 80000000, Data type is System.Int32
  5. Verify that the second case of -2147483648 is incorrectly treated as Long.
  6. Press the ENTER key to close the Console window.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbCompiler kbprb KB811402 kbAudDeveloper