var

Variables that are declared at method scope can have an implicit type "var".
This cannot be used in parameter lists
This cannot be used in a class field
This implicitly typed local variable is strongly typed the only difference being that the compiler determines the type
Implicitly typed variable declarations must include an initializer.

var myvar = 10;   // implicitly typed 
int myvar = 10; // explicitly types

Implicitly Typed Arrays

You can create an implicitly typed array in which the type of the array instance is inferred from the elements specified in the array initializer.
The rules for an implicitly typed variable also apply to implicitly typed arrays

var myarray = new[] {1,2,3,4} 
var myarray = new[] {"one","two","three"}

Object not Variant

There is no Variant datatype in C# or VB.NET. The closest datatype in .NET is Object.
When using the VB 2005 Code Editor any code that declares a variable as an Variant or does not specify the data type, it is automatically converted to an Object type.
The variant datatype is the datatype that can contain any kind of data except fixed-length string data and user defined types. Using the variant datatype can provide a more flexible way to work with data. You can determine how the data in a variant is treated using the VarType function or TypeName function.


An error occurs when the variant variables containing Curency, Decimal and Double values exceed their respective ranges.
The value Empty denotes a variant variable that has not been assigned to an initial value. A Variant containing Empty is 0 if it is used in a numerical context and a zero length string ("") if used in a string context.


The value Null denotes a variant variable that intentionally contains no data. The Value Bull denotes that an erro condition has occurred. Normal error handling does not occur giving you the chance to take alternative action in code


The variant datatype can also contain the special values "Error", "Null", "Nothing" and "Empty". There are also the corresponding functions IsNull, IsError and IsEmpty


An object variable has not been set if it is assigned to "Nothing".


A variant can represent Empty, Error, Nothing and Null.
You can use the TypeName() function to return the type of a variable


The variant datatype can be used for all datatypes except fixed length strings and user defined types.


Variant - VB.Net

Using the Variant datatype is generally a waste to memory since Variants require more memory than other datatypes.
The following two lines of code are equivalent.

Dim sFirstName 
Dim sFirstName As Variant

Using a Variant datatype may be useful sometimes but generally this should be avoided.
Because you know that FirstName will always contain a string you should declare this variable as:

Dim sFirstName As String 


© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext