Variables
Also known as Identifiers.
A variable is just a named storage location in memory. At the top of all your modules you should have "Option Explicit" to force explicit declaration.
Variables are case sensitive in C#.
Variables that are not defined explicitly have the "Variant" data type assigned to them by default.
Variables can accommodate a wide variety of data types from simple boolean values to extremely large 20 digit numbers.
In C# assigning values to variables and comparing values are treated as two very different operations.
Because of this they have different symbols
= - for assignment
== - for comparison
In VB this is the same symbol (=).
Prefix private variables with an underscore "_"
Use Pascal casing for public variables
Use Hungarian notation for private or local variables
Always try and declare your local variables at the very top of the method and not amongst lines of code
Do not use language specific type names. For example use Int16 instead of Short
Inside or Outside
Do you declare the variables inside or outside the try block.
public bool DoSomething(
string sPassedIn)
{
Excel.Worksheet oWorksheet;
float myNumber;
try
{
Lifetime and Scope
The difference between lifetime and scope is quite simple.
Lifetime - Refers to how long or when the variable is valid (i.e. how long will it retain its value for).
Scope - Refers to where the variable can be accessed.
The difference between lifetime and scope is quite simple.
lifetime is how long the variable retains its value for.
The scope refers to where the variable can be used.
Lifetime vs Scope
The lifetime of the local variable "imylocal" extends from the moment ProcedureOne is entered to the moment ProcedureOne has finished.
However the local variable "imylocal" still exists when ProcedureTwo is called, it is just out of scope.
Public Sub ProcedureOne()
Dim imylocal As Integer
imylocal = 5
Call ProcedureTwo()
imylocal = 10
End Sub
Public Sub ProcedureTwo()
'do something
End Sub
In VB it was very important to set your Object references to Nothing when you have finished with the object.
In C# however it is sufficient to wait until the object references goes out of scope.
C# makes use of a very efficient garbage collection mechanism.
If you do want to set them to Nothing then the equivalent is null.
Variable Scope
The scope controls the lifetime and visibility of the variable. It is good programming practice to use the most restrictive scope possible for your variables.
You can also declare variables using the public and private keywords to explicitly indicate this scope.
Procedure / Local - variables and constants are local and are declared either using "Dim" or "Static" at the start of a procedure. A local (or procedure-level) variable or constant is declared within the procedure and is not visible outside of that procedure.
Module ("m" prefix) - variables and constants are declared as Private at the top of a module (always preceded by m). Module level variables are declared in the declaration section of a code module (standard, class or userform). These variables should be used if you want several procedures to have access to the same variable.
Global ("g" prefix) - variables are declared as Public at the top of a module level (always preceded by g). Public variables constants, procedures and functions are visible from any project that references the project where they are declared.
Every variable has a scope associated with it.
This scope refers to the area of code where the variables will be recognised.
Variables (and constants) have a defined scope within the program.
The scope for a variable can either be:
Procedure Level - Also known as a local variable.
Module Level Private - A private module level variable is visible to only the module it is declared in.
Module Level Public - A public module level variable is visible to all modules in the project.
Procedure Level
A local or procedure level variable is declared inside an individual procedure or function and is not visible outside that subroutine
Local variables can only be used in the procedure in which they are declared in.
When the procedure or function ends the variable is automatically removed and the memory is released.
You can use the Dim, Static or Private statement within a subroutine or function.
The most common way to declare a local variable is to use the Dim statement between the Sub and End Sub statements.
One of the great advantages of local variables is that we can use the same name in different subroutines without any conflicts.
Public Sub Procedure_Name()
Dim slocalvariable As String
End Sub
One of the advantages of local variables is that you can use the same name in different subroutines without any conflict.
Module Level (Private)
A module level variable is declared in the declarations section of a code module (standard, class or userform).
Module level variables can have two flavours either Public or Private.
A private module level variable is visible to only the module it is declared in.
If you declare a module level variable with the Dim statement this is equivalent to using the Private statement.
Is this the same for constants ??
This often causes a lot of confusion and should definitely be avoided.
Private sModuleLevel As String
Dim sModuleLevel As String
Public Sub Procedure_Name()
End Sub
Module Level (Public)
A public module level variable is visible to all modules in the project.
These are also know as project level or global variables
You must insert these statements at the very top of your modules, before any procedures or functions.
Project scope variables must aslo be declared in a standard code module and not is a userform or class module.
Public sProjectLevel As String
Public Sub Procedure_Name()
End Sub
Declaring
Should they be declared at the very top or inline ??
Variable Names
The first character must always be alphabetic.
You cannot use spaces or periods.
Variable names can use alphabetical characters, number and some punctuation characters but not ( * . , # $ % & !).
The use of the underscore character is also encouraged.
The name must start with a letter and can have a maximum of 254 characters.
A variable must begin with a letter or an underscore
Variable names can be constructed from letters and numbers and the underscore character.
Variable names cannot be the same as C# keywords.
Case Sensitive
Multiple Variables
string firstName, lastName;
This statement is valid although the variable sFirstName is defined as a Variant variable and not as a string variable.
Dim sFirstName, sLastName As String
VBA does not let you declare a group of variables to all be of the same data type.
You must specify the type of each variable explicitly.
You should always try and declare each variable on a separate line
Dim sFirstName As String
Dim sLastName As String
You could put this on one line
Dim sFirstName As String, sLastName As String
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 = ""
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
Assigning
After a variable has been declared (and initialised with its default value) it can be assigned a different value.
Most script languages use late binding, and compiled languages use early binding.
Early Binding
Also known as Static Binding
It recognizes and checks the methods, or properties during compile time.
Non-virtual methods are always early bound.
Everything is early bound in C# unless you go through the Reflection interface.
Late Binding
Added in .NET 4.0
Also known as Dynamic Binding
Here the compiler does not know about what kind of object it is and what the methods or properties are until run-time.
Late binding is achieved by using virtual methods.
The performance of late binding is slower than early binding because it requires lookups at run-time.
Virtual methods are always late bound
Prior to that you could use the reflection API to do it.
Discards
Discards are placeholder variables that are intentionally unused in application code.
Discards are equivalent to unassigned variables; they don't have a value.
A discard communicates intent to the compiler and others that read your code:
You intended to ignore the result of an expression.
You may want to ignore the result of an expression, one or more members of a tuple expression, an out parameter to a method, or the target of a pattern matching expression.
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext