VB.Net - Syntax


Static Variables

Also known as Static method variables or method level static variables
These are not supported in C#.
The equivalent in C# would be a private static variable to the class
VB.Net static local variables are not thread safe


Static variables are not used very often but they can be very useful when used in the correct way.
Variables do have a lifetime.
Indicates that the local variable is preserved between calls.
A static variable can be thought of as a local variable with memory.
A static variable is a local variable whose lifetime is the lifetime of the entire module and not the procedure where it is declared.
In fact static variables retain their values as long as the code module is active. There does not have to be any code running all the time.
Therefore a static variable has the scope of a local variable but the lifetime of a module level variable.
Makes a variable persistent even after the procedure has completed.
The next execution of the routine can access the previous value.
It is the lifetime of a variable that determines its existence, the scope determines its visibility.


Private Module Level Variables

You can obtain the same effect using a private module level variable instead of a static variable.
It is considered better programming practice to use static variables as this uses a more restictive scope.
This helps to prevent accidental changes being made to the variable in other portions of the code.


Simple Example

Using "static variables" lets you retain the value of a variable that may go in and out of scope during execution, yet remain valid.
A static variable has the scope of a local variable although the lifetime of a module level variable.

Public Sub Procedure_One() 
   Debug.Print Function_Two()
   Debug.Print Function_Two()
   Debug.Print Function_Two()
   Debug.Print Function_Two()
   Debug.Print Function_Two()
End Sub

Public Function Function_Two() As Integer
Static iValue As Integer

   iValue = iValue + 1
   Function_Two = iValue
End Function

It is possible that a variable can go in and out of scope and yet remain valid during that time (ie keep its value).
Once the lifetime of the variable expires the value is lost.
It is the lifetime that determines the existence of a variable and it's the scope that determines its visibility.
Indicate that the variable is initialised the first time and is then preserved between function / procedure calls.
A static variable is a local variable whose life time is the lifetime of the entire module, not just the procedure


Static Procedures and Functions

To make all local variables in a procedure or function static, place the static keyword at the beginning of the procedure or function heading (eg Static Sub or Static Function).


Private Static Sub Procedure_One() 
Dim slocalvariable As String

End Sub

With End Block

There is no direct equivalent to this in C#

oHttpWebRequest.Method = "GET"; 
oHttpWebRequest.TimeOut = 10000

You can use object initializers

MyClass myObject = new SuperHero(); 
myObject = new MyClass() {Name="Text", Value=3 }

With oHttpWebRequest 
.Method = "GET"
.TimeOut = 10000
End With


Dim myObject As MyClass; 
myObject = New MyClass;

With myObject
.Name = "Text"
.Value = 3
End With

Option Strict

This is switched off by default but actually appears as "(custom)"
The default is off which is to allow implicit type casting (or late binding).
When switched on it will prevent narrowing conversions without explicit casting.


Right click on your project and select "Properties"
Display the Compile tab
SS


This setting does not exist in C#, because implicit data type converson is not allowed.
This is a new setting in VB.NET and is switched off by default.
Ensures you do not perform any unsafe type conversions
With Option Strict On you cannot perform any narrowing conversions
(i.e. you cannot cast a variable from Long to Integer without explicitly using a conversion function)
Widening is always allowed.



Special reference should be made to the function CType()
This function allows us to temporarily convert a variable of one Reference type (usually object) into another Reference type.
When Option Strict is on this is the only way to convert a variable of type Object into a more specific data type.


All objects are early bound by default
Early binding means our code is checked at compile time (before run-time errors can occur) to determine whether a certain object has the correct properties and methods.
Intellisense is a direct consequence of early binding.


If Option Strict is On, when you pass in a Single with a decimal part (eg 3.45) you must append the letter F (3.45F) to indicate to the compiler that the value is a Single and not a Double.


Option Explicit

This setting does not exist in C#, because variables must always be declared
[include link ??]


The default is on and this is not included in your source code.
Determines if you have to declare all your variables before they are used.


C# requires all variables to be declared and there is no way of turning this off.
In C# all variables must be declared before they are used.


Explicit Declaration / Option Explicit

VBA is not a "strictly typed" programming language so you can get away with not explicitly declaring the data type of your variables.
This is very bad practice and leads to code that is slow and unreadable.
There is an option though that can force you to explicitly declare the data type of each variable and this should be switched on.
Check your (Tools > Options)(Editor tab, "Require Explict Declaration") and make sure it is checked.
Selecting this check box will automatically add the statement Option Explicit to any new modules (not existing ones).


Option Explicit 

When this statement is used you must explicitly declare all your variables before using them.
This ensures that a program will not run if it contains any variables that have not been explicitly declared.


You declare variables by using the "Dim" keyword.
Local variables are the most efficient because VBA frees up the memory when the procedure or function executes


Any variables that are not specifically declared are assigned as a "Variant" data type. VBA automatically converts the data to the proper type when it is used.


The variant data type generally wastes memory although there is another reason why all your variables should be explicitly declared.
This is related to preventing typing errors.
If you accidently misspell a variable name (which can happen) a new variable will not be created if your variables have all been explicitly declared.
This can lead to some nasty Run-time errors and unpredictable results.


Variables should always be declared explicitly as it ensures that spelling mistakes are picked up and ensures that memory is used efficiently.
The "Variant" data type uses a minimum of 16 bytes and is very slow. Using a list of comma-separated variables does not work.
You do not have to explicitly declare your variables, you can let Visual Basic do this for you.
Any variables that are created automatically have the "Variant" data type and can contain any type of data.
VBA automatically converts the data to the proper type when it is used.
You should explicitly initialise the variables and not rely on the system defaults.
You code will run a lot slower since a variant data type uses up more memory.


Option Infer

This is switched on by default.
This is used extensively with LINQ statements
This enables the user of local type inference when declaring variables.
When this is On, you can declare local variables without explicitly stating their data type.


New to VB in Visual Studio 2008
The default is on.
If you have Option Explicit switched off then whether a variable has the old behaviour or new is determined by this setting ???


Turning Option Infer On allows old VB6 code full of variants to compile and run.
To set Option Infer in a file type type Option Infer On at the top of the file, before any source code
If this value conflicts with the setting in the IDE then the value in the file has precedence.
The compiler infers the data type for a variable when it is initialised.



Project Settings

Right click on your project and select "Properties"
Display the Compile tab
SS


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