Data Types

KeywordTypeSystem.SuffixVB.NetComments
boolBooleanSystem.Boolean Boolean(1 bit) true or false
stringCharacterSystem.String StringReference Type
charStringSystem.Char Char(16 bits) Unicode character (0 to 65,536 different characters)
\u 000 to \u FFFF
n/aDateSystem.DateTime Date(64 bits) - Jan 1 100 to Dec 31 9999
floatFloating PointSystem.Singlef or FSingle(32 bits, 4 bytes) Single Precision Floating Point
-3.402 E 38 to 3.4.02 E 38, precision ~6-9 digits
doubleFloating PointSystem.Doubled or D
(R in VB.Net)
Double(64 bits, 8 bytes) Double Precision Floating Point
-1.797 E 308 to 1.797 E 308, precision ~15-17 digits
decimalFloating PointSystem.Decimalm or M
(D in VB.Net)
Decimal(128 bits, 16 bytes) Precise Integer and Fractional type
Can represent decimal numbers with 29 significant digits
1 E -28 to 7.9 E 28
Currency in VBA
sbyteIntegerSystem.SByte  (8 bits) -128 to 127
byteIntegerSystem.Byte Byte(8 bits) Unsigned Integer 0 to 255
shortIntegerSystem.Int16 Short(16 bits) -32,768 to 32,767
Integer in VBA
ushortIntegerSystem.UInt16  (16 bits) Unsigned Integer 0 to 65,535
intIntegerSystem.Int32 Integer(32 bits) -2,147,483,648 to 2,147,483,647
uintIntegerSystem.UInt32u or U (32 bits) Unsigned Integer 0 to 4,294,967,295
longIntegerSystem.Int64l or LLong(64 bits) -9.2 E 18 to 9.2 E 18
ulongIntegerSystem.UInt64ul or UL (64 bits) Unsigned Integer 0 to 1.84 E 19
object System.Object ObjectReference Type
n/a System.Tuple  Reference Type

Intrinsic vs User Defined

Intrinsic are built-in
It is always best to use the data type that uses the smallest amount of space (i.e. bytes) yet can still handle all the data assigned to it. The smaller the bytes the faster it executes.
You should use the data type conversion functions instead of using the Val() function when you want to convert from one data type to another.

  • Boxing - Forcing value types to act as reference types

  • Casting - Converting objects of one type into another type

Casting can either be narrowing or widening


Dynamic Types


VBC#
If (myObject IsNot Nothing) ThenIf (!myObject.Equals(null))
End If{
 }
  
TypeName(x)x.GetType.ToString();
  
GetType(Integer)typeof(int);
  
nothingnull

DateTime is not a built-in C# type


Initialising

VBC#
Dim b As Byte = &H2A 'hexbyte o = 0x2A;
Dim o As Byte = &O52 'octal 
Dim ed As Date = #12/31/2007 12:15:00 PM# 
Dim amount As Decimal = 23.99@decimal amount = 23.99m;
Dim number As Single = 2.9!float number = 2.9f;
Dim total As Long = 23432L 


System.Convert.ToInt16(sNumber) 
System.Convert.ToDouble(sNumber)

Type.GetTypeCode()     = TypeCode.String 


Decimal instead of Currency

The Currency data type in VBA has been replaced with the Decimal data type in VB.NET.
In most cases these will be interchangeable however they do actually differ in their precision.
The Decimal data type in .NET is more accurate than the VBA Currency data type
The Decimal data type is a fixed-precision of the decimal point.
Decimal values require the suffix "m" or "M".
You can force a data type to a literal value to Decinal by adding "D" to the end of the literal value. For example 2344534D is forced to be interpreted as a Decimal, without the "D" the literal value is interpreted as a Long.



User Defined Data Types


You define custom data types outside of procedures at the top of your module. Once you have created your type use the Dim statement to declare a variable of that type.


This method allows you to define an object that encapsulates and validates its own data.
Remember: An object variable declared "As New" contains "Nothing" until the first time you refer to it in code


After you have finished with any objects you must set any refernces to it to "Nothing" in order to free the memory space & resources
Perhaps the most common is a structure to pass to an API call. You may also have to pass in parameters that are strings are finite length



Important

It is possible to create your own user defined types by usingthe "Type" statement. This is covered later
When defining a user defined type all variables within the type should be in capitals


© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext