Help and Support

INFO: How VB Interprets Numbers, Constants and Numeric Types

Article ID:199809
Last Review:July 13, 2004
Revision:2.1
This article was previously published under Q199809

SUMMARY

Visual Basic interprets numbers, numeric constants and data types in expressions in a way that may cause unexpected results if you are not aware of the way they are handled. This article gives some background on this process and explains how to guarantee the correct results when using expressions.

Back to the top

MORE INFORMATION

Visual Basic stores data as the smallest possible data type if no explicit type is specified. For example, if you type in the following statement 50 is temporarily stored as an integer (2 bytes), internally, regardless of the type specified for x:

x = 50
				

This can lead to unexpected results when you use the following code sequence:


Dim x as Long

x = 24 * 24 * 60

				

This statement generates an overflow error, since 24 * 24 * 60 = 34560, which exceeds the maximum size of a 2 byte integer (32767). Visual Basic does not evaluate the entire expression to check for the size of the result, but instead continues to use a 2 byte temporary space for the calculation. The same overflow error occurs if you declare the preceding values as constants and multiply the constants.

In order to work around this behavior, it is important to always explicitly type numbers when using them in numeric calculations or when defining constants. If the preceding expression is changed to the following:


Dim x as Long

x = 24& * 24& * 60&

				

No error occurs. This is because the & character is used to explicitly type the numbers as long integer types (4 bytes). Similarly, if you declare a numeric constant that will be used in a numeric calculation, you should explicitly type the declaration, as follows:

Const X as Long = 50
				

-or-

Use the older format:


Const X  = 50&

				

This avoids any unexpected results.

There is one other time when it is very important to understand how Visual Basic treats these numbers internally:

When you are passing values to an API function.
If the API function is expecting a long integer for a parameter, and you need to pass a value of 0, you should always pass in the value 0&. This ensures proper byte alignment of the parameter list that is sent to the API function. Visual Basic correctly performs this conversion for you under most circumstances, but explicitly specifying the size of the value eliminates the potential risk. For example:

Declare Function MyFunc Lib "MyDll.dll" (i as Any) as Long
				

Here you need to call the function as follows, which is equivalent to passing a NULL pointer:


k = MyFunc(ByVal 0&)

				
If you use 0 instead of 0& your application may crash.

Back to the top

REFERENCES

For additional information on how Visual Basic treats data types internally, please see the following article in the Microsoft Knowledge Base:

129803 (http://support.microsoft.com/kb/129803/EN-US/) Variable Coercion Rules in Visual Basic

Back to the top


APPLIES TO
Microsoft Visual Basic 4.0 Standard Edition
Microsoft Visual Basic 4.0 Professional Edition
Microsoft Visual Basic 4.0 16-bit Enterprise Edition
Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
Microsoft Visual Basic 5.0 Learning Edition
Microsoft Visual Basic 6.0 Learning Edition
Microsoft Visual Basic 5.0 Professional Edition
Microsoft Visual Basic 6.0 Professional Edition
Microsoft Visual Basic 5.0 Enterprise Edition
Microsoft Visual Basic 6.0 Enterprise Edition

Back to the top

Keywords: 
kbinfo KB199809

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Contact Microsoft
    Phone Numbers, Support Options and Pricing, Online Help, and more.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.