4 bytes 32 bit architecture, 8 bytes 64 bit architecture
Dim example As Object = Nothing
There also exist data identifier and literal type characters usable in replacement for the textual type and or to force literal type:
Type (or Alias)
Identifier type character
Literal type character
example = 10S
example = 10% or example = 10I
example = 10& or example = 10L
example = 10! or example = 10F
example = 10# or example = 10R
example = 10@ or example = 10D
example = "A"C
example = 10US
example = 10UI
example = 10UL
The integral suffixes are also usable with hexadecimal (&H) or octal (&O) prefixes: example = &H8000S or example = &O77&
Date(Time) objects can also be defined using literal syntax: Dim example As Date = #7/26/2016 12:8 PM#
Once a variable is declared it will exist within the Scope of the containing type, Sub or Function declared, as an example:
The counter variable will only exist until the End Function and then will be out of scope. If this counter variable is needed outside of the function you will have to define it at class/structure or module level.
Alternatively, you can use the Static (not to be confused with Shared) modifier to allow a local variable to retain it's value between calls of its enclosing method:
Example of Access Modifiers
In the following example consider you have a solution hosting two projects: ConsoleApplication1 and SampleClassLibrary. The first project will have the classes SampleClass1 and SampleClass2. The second one will have SampleClass3 and SampleClass4. In other words we have two assemblies with two classes each. ConsoleApplication1 has a reference to SampleClassLibrary.
See how SampleClass1.MethodA interacts with other classes and methods.
Levels of declaration – Local and Member variables
Local variables - Those declared within a procedure (subroutine or function) of a class (or other structure). In this example, exampleLocalVariable is a local variable declared within ExampleFunction():
The Static keyword allows a local variable to be retained and keep its value after termination (where usually, local variables cease to exist when the containing procedure terminates).
In this example, the console is 024. On each call to ExampleSub() from Main() the static variable retains the value it had at the end of the previous call:
Member variables - Declared outside of any procedure, at the class (or other structure) level. They may be instance variables, in which each instance of the containing class has its own distinct copy of that variable, or Sharedvariables, which exist as a single variable associated with the class itself, independent of any instance.
Here, ExampleClass2 contains two member variables. Each instance of the ExampleClass2 has an individual ExampleInstanceVariable which can be accessed via the class reference. The shared variable ExampleSharedVariable however is accessed using the class name:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0