## Data types in VB and .NETThis article presents a technical look into the numeric data types supported by classic Visual Basic, VBA, VB.NET and the .NET framework. There are surprising peculiarities in several types. A conversion diagram shows how to achieve lossless type conversion. How to store numbers in your programs? How to avoid Overflow or loss of decimals? How to work with dates? This article summarizes the elementary numeric data types in classic Visual Basic, Visual Basic for Applications, Visual Basic .NET and other .NET languages. Reasonable development experience is assumed. This is no introduction to programming. You may find some of the information especially handy if you intend to migrate from VB6 to a .NET language. ## Data type summaryWith numeric data types we mean the elementary built-in types intended to store integer numbers, decimal numbers, flags and dates. The following table shows numeric data types sorted by their storage requirements in bits and the numeric range they support. The VB6/VBA data types are used in classic VB and Visual Basic for Applications. The .NET data types are offered by the .NET framework versions from 2.0 to 4.0. The VB.NET data types are aliases for the underlying .NET data types, available in Visual Basic versions from 2005 to 2010.
* Decimal: ±79,228,162,514,264,337,593,543,950,335 ## Data type specifics## BooleanBoolean is really a flag and not a numeric data type. The storage size is 16 bits in VB6 and varies by platform in .NET. ## Integer and LongInteger and Long have the same name and type character in VB6/VBA and VB.NET, but they are different data types altogether. To stay clear one can use Int16, Int32 and Int64 in .NET. ## CurrencyCurrency is a 64-bit integer divided by 10000. It is only available in VB6/VBA. In VB.NET one can replace it with Decimal, which provides more capability. ## LongLongIntroduced with Office 2010, LongLong is a 64-bit signed integer that is available in VBA7 running on a 64-bit platform. It is unsupported in VB6, VBA6 and on 32-bit platforms. ## DecimalDecimal is a signed 96-bit integer, which is divided by a scaling factor. The scaling factor varies from 1 to 1E+28. A Decimal can represent 29 decimal digits: integers and decimal numbers with max 28 decimal places. The binary representation consists of a 1-bit sign, a 96-bit integer number and the scaling factor. The smallest nonzero Decimal is ±1E−28 = ±0.0000000000000000000000000001. The storage size is 128 bits, but not all the bits are used for the number. VB6 supports Decimal with the Variant data type. You cannot declare a variable to be of type Decimal. You can, however, create a Variant whose subtype is Decimal using the ## SingleThe value closest to zero is ±1.401298E-45. VB6: The maximum Single is 3.4028235E+38. It rounds down to 3.402823E+38 when displayed. The latter is also the maximum Single literal you can write in VB6 code. .NET: ## DoubleThe value closest to zero is ±4.94065645841247E-324 in VB6 and ±4.94065645841246544E-324 in .NET. VB6 stores max 1.7976931348623157E308. It rounds down to 1.79769313486231E+308 when displayed. The latter value is also the maximum literal number you can write in VB6 code. The absolute maximum is 1.7976931348623158077E308, which equals 1.7976931348623157E308 due to rounding. .NET: NaN, PositiveInfinity and NegativeInfinity are special values of Double. ## Date and time valuesDates and times are closely related to numeric data types, especially in VB6. ## VB6 date and timeVB6 stores dates and times in the Date data type. Date can store either the calendar date, the time of day, or both. The range covered is midnight The integral part of a Date represents the day. The value zero is equal to The time of day is in the decimal fraction. An hour is equal to 1/24, a minute is 1/1440 and a second is 1/86400. Noon is 0.5. Noon on ## Peculiarities with historical dates in VB6The integral part is the date, the fraction is the time: With modern dates time always runs forwards, as you would suspect. With negative historical dates time actually runs backwards! Midnight To make things worse, time values for Another surprise is here: ## Expressions for VB6 date/timeGood expressions: Avoid these: Bug with DateDiff: ## .NET date and timeDateTime represents dates and times with a 64-bit value, which includes 62 bits for the date/time and 2 bits for time zone information (the Kind property). The range covered is midnight TimeSpan represents a time interval (duration), either positive or negative. It can also represent the time of day unrelated to a particular date. TimeSpan is a 64-bit type whose underlying numeric range is equal to that of Int64. Time is stored as ticks of 100 nanoseconds each. The range covered is −10675199.02:48:05.4775808 to 10675199.02:48:05.4775807 as measured in days, hours, minutes, seconds and fractions of a second. This is approximately ±29228 years, which means a TimeSpan can go over the date range supported by DateTime. DateTimeOffset is a structure representing a DateTime value along with its time difference to UTC. This structure is outside the scope of this article. ## Data type conversion diagramThe following diagram shows the possible data type conversions in VB6 and .NET. There are two considerations when converting a value from a data type to another: magnitude and precision. - Magnitude: An error may occur if the converted value is too large (or too small) to fit in the target data type.
- Precision: Loss of precision may occur when converting to a Single or Double. In this case, magnitude is preserved, but some less significant digits may be lost. Loss of precision does not cause a run-time error but it may lead to erroneous results.
Data types in VB and .NET |