Data Types
C# | VB.Net | Type | Suffix | Comments |
bool | Boolean | System.Boolean | (1 bit) true or false | |
byte | Byte | System.Byte | (8 bits) Unsigned Integer 0 to 255 | |
char | Char | System.Char | (16 bits) Unicode character (0 to 65536 different characters) \u 000 to \u FFFF | |
n/a | Date | System.DateTime | - Jan 1 100 to Dec 31 9999 | |
decimal | Decimal | System.Decimal | m or M (D in VB.Net) | (96 bits) Precise fractional or integral type that can represent decimal numbers with 29 significant digits 1 E -28 to 7.9 E 28 Currency in VBA |
double | Double | System.Double | d or D (R in VB.Net) | (64 bits) Double Precision Floating Point -1.797 E 308 to 1..797 E 308 |
float | Single | System.Single | f or F | (32 bits) Single Precision Floating Point -3.402 E 38 to 3.4.02 E 38 |
int | Integer | System.Int32 | (32 bits) -2147483648 to 2147483647 | |
long | Long | System.Int64 | l or L | (64 bits) -9.2 E 18 to 9.2 E 18 |
object | Object | System.Object | Reference Type | |
sbyte | n/a | System.SByte | (16 bits) -128 to 127 | |
short | Short | System.Int16 | (16 bits) -32,768 to 32,767 Integer in VBA | |
string | String | System.String | Reference Type | |
uint | n/a | System.UInt32 | u or U | (32 bits) Unsigned Integer 0 to 4,294,967,295 |
ulong | n/a | System.UInt64 | ul or UL | Unsigned Integer 0 to 1.84 E 19 |
ushort | n/a | System.UInt16 | Unsigned Integer 0 to 65535 | |
System.Tuple | System.Tuple |
Integers use modulo arithmetic, so no overflow can occur
The default value type for any literal is "int"
Division follows standard "integer division" rules when using integer types
Division by zero causes an exception for integer types (DivideByZeroException)
Division by zero results in infinity for floating point types
int a = 7
int b = 2
int c = a/b ' c=3 not 3.5
float d = 4
float e = 3
float f = d/e ' f = 1.33333
float g = 1.0F / 0.0F ' g=positive infinity
float h = -1.0 / 0.0 ' h=negativeinifnity
float j = 0.0F / 0.0F ' j = NaN
Wrapping - No Overflow
int a = 2147483647;
int b = 1;
int sum = a + b;
'wraps to sum = -2147483648
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
VB | C# |
If (myObject IsNot Nothing) Then | If (!myObject.Equals(null)) |
End If | { |
} | |
TypeName(x) | x.GetType.ToString(); |
GetType(Integer) | typeof(int); |
nothing | null |
DateTime is not a built-in C# type
Initialising
VB | C# |
Dim b As Byte = &H2A 'hex | byte 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