You receive an "Overflow" error if you use -9223372036854775808 (Long.MinValue) in Visual Basic .NET or in Visual Basic 2005 (820657)



The information in this article applies to:

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

SYMPTOMS

If you use -9223372036854775808 for the minimum value of the Long data type in your Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 application, you cannot build your application, and you receive the following error message:
Overflow.

CAUSE

This problem occurs because Microsoft Visual Basic .NET, Microsoft Visual Basic 2005, Microsoft Visual C# 2005, and Microsoft Visual C# .NET have syntactic grammar that does not include the leading hyphen (-) as part of the numeric literal. The parser reads the numeric part, and then the parser applies the unary minus (-) operator to the number. Your Visual Basic .NET application parses the minimum value of the Long data type as 9223372036854775808. This number is greater than maximum value for the Long data type. Therefore, you receive the error message mentioned in the "Symptoms" section of this article.

Note The problem only occurs in Visual Basic .NET or in Visual Basic 2005, because Visual C# .NET or Visual C# 2005 has a rule for the numeric type hyphen prefix.

WORKAROUND

To work around the problem, use one of the following procedures:
  • Use the MinValue property of the data type, as in the following example:
    Dim lValue as Long = Int64.MinValue
    -or-
  • Use the numeric expression that evaluates to the minimum value for the Long data type. For example, instead of using -9223372036854775808, use -9223372036854775807 -1, as in the following code:
    Dim lValue as Long = -9223372036854775807 -1

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a console application by using Visual Basic .NET or Visual Basic 2005.
  2. Copy the following code to the Sub Main method:
    Dim lValue As Long = -9223372036854775808
  3. On the Build menu, click Build Solution.

    You receive the error mentioned in the "Symptoms" section of this article.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

811402 Microsoft Visual Basic .NET Parser Incorrectly Evaluates the Data Type If the Minimum Value of the Numeric Data Types Is Parsed


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