Microsoft KB Archive/811402

= 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 =

Article ID: 811402

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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  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,  is parsed as , and   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, use.



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
 Create a new Visual Basic .NET or Visual Basic 2005 Console Application project.

By default, is created.  Add the following code to the Sub Main method of : ' By using an Expression that results to Minimum Value of Integer data type Console.WriteLine(&quot;1) Hex(-2147483647 - 1) = {0}, Data type is {1}&quot;, Hex(-2147483647 - 1), (-2147483647 - 1).GetType.ToString) ' By directly using the Minimum Value of Integer data type Console.WriteLine(&quot;2) Hex(-2147483648) = {0}, Data type is {1}&quot;, Hex(-2147483648), (-2147483648).GetType.ToString)

' By using MinValue method of Integer data type to get Minimum Value Console.WriteLine(&quot;3) Hex(Integer.MinValue) = {0}, Data type is {1}&quot;, Hex(Integer.MinValue), (Integer.MinValue).GetType.ToString)

Console.Read  On the Debug menu, click Start to run the sample code.  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  Verify that the second case of  is incorrectly treated as Long. Press the ENTER key to close the Console window.</li></ol>

Keywords: kbvs2005swept kbvs2005applies kbcompiler kbprb KB811402

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.