Data Types

C#VB.NetTypeSuffixComments
boolBooleanSystem.Boolean (1 bit) true or false
byteByteSystem.Byte (8 bits) Unsigned Integer 0 to 255
charCharSystem.Char (16 bits) Unicode character (0 to 65536 different characters)
\u 000 to \u FFFF
n/aDateSystem.DateTime - Jan 1 100 to Dec 31 9999
decimalDecimalSystem.Decimalm 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
doubleDoubleSystem.Doubled or D
(R in VB.Net)
(64 bits) Double Precision Floating Point -1.797 E 308 to 1..797 E 308
floatSingleSystem.Singlef or F(32 bits) Single Precision Floating Point -3.402 E 38 to 3.4.02 E 38
intIntegerSystem.Int32 (32 bits) -2147483648 to 2147483647
longLongSystem.Int64l or L(64 bits) -9.2 E 18 to 9.2 E 18
objectObjectSystem.Object Reference Type
sbyten/aSystem.SByte (16 bits) -128 to 127
shortShortSystem.Int16 (16 bits) -32,768 to 32,767
Integer in VBA
stringStringSystem.String Reference Type
uintn/aSystem.UInt32u or U(32 bits) Unsigned Integer 0 to 4,294,967,295
ulongn/aSystem.UInt64ul or ULUnsigned Integer 0 to 1.84 E 19
ushortn/aSystem.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 datatype 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 datatype conversion functions instead of using the Val() function when you want to convert from one datatype 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

Syntax > Explicit Casting






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 datatype in VBA has been replaced with the Decimal datatype in VB.NET.
In most cases these will be interchangeable however they do actually differ in their precision.
The Decimal datatype in .NET is more accurate than the VBA Currency datatype
The Decimal datatype is a fixed-precision of the decimal point.
Decimal values require the suffix "m" or "M".
You can force a datatype 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 datatypes 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
There are some differences if you are using C#, for more details please refer to the C# Differences > Datatypes
When defining a user defined type all variables within the type should be in capitals


© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

Next