Initialising

After a variable has been declared it can be initialised with a default value. This is called initialising.
When a procedure or function begins execution, all of its local variables are automatically initialised.
Explicitly initialising all your local variables makes your code more readable and is a good habit to get into.
These default values can be found in the data types table earlier.
Implicitly-typed variables must be initialized


No Default Values

In C# there are no default values assigned to your variables.
C# does not assign any default values to local variables when they are declared.
Instead it requires you to always initialise the variables before they are used
A compilation error will occur if you try to read a value from a local variable before it has been initialised.


VB however assigns default values to all its local variables.
VB automatically initializes all variables to 0 or nothing.


When a procedure or function begins execution, all of its local variables are automatically initialised, that is given an initial value.
Explicitly initialising all your local variables makes your code more readable and is a good habit to get into.
Static variable however cannot be initialised, due to there fundamental purpose.
These default values can be found in the data types table


Numeric variables (integer, short, long, double, currency) are initialised to zero
Variable length strings are initialised to a zero-length (or empty) string
Fixed length strings are initialised with the character represented by the ASCII code 0, or Chr(0).
Variant variables default to Empty
Object variables default to Nothing


When a subroutine starts all the local variables are automatically initialised to their default values.


string firstName = "", lastName = ""; 

Dim sFirstName As String 
'the automatic initialisation will set its initial value to the empty string ""

It is not good programming practice to rely on these automatic initial values.
You should always try and explicitly specify the initial value.

Dim sFirstName As String 
sFirstName = ""


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 


It is common to declare a variable and initialise it all on the same line.
If the compiler can automatically work out the data type based on how it is being initialised you can use the "var" keyword.
These implicitly typed variables are statically typed which means the data type cannot be changed afterwards (eg at runtime)

var myVar = 0.5; 
var myVar = 0.5F

Also known as numeric literal suffixes.
In most cases the compiler is able to determine the correct data type for a numerical literal however there might be times when the default data type is different to the desired one.
In these cases a suffix can be added to explicitly tell the compiler which data type you want to use.

myNumber = 10000000F  // float = System.Single 
myNumber = 10000000U // uint = System.UInt32
myNumber = 10000000L // long = System.Int64
myNumber = 10000000UL // ulong = System.UInt64
myNumber = 10000000M // decimal = System.Decimal

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