A variable is a named memory location. They are programming elements that can change
during program execution. Data that needs to be stored in memory & accessed at a later
time are stored in variables. Instead of referring to the memory location by the actual
memory address you refer to it with a variable name.
Variables are declared as follows
Dim a as Integer
They can also be initialized at the time of declaration as follows:
Dim a as Integer = 10
Constants are very similar to variables. The main difference is that the value contained in
memory cannot be changed once the constant is declared. When you declare a constant
its value is also specified and this value cannot be changed during program execution.
Constants are used in situations where we need to keep the value in some memory
location constant. If you use hard-coded values, and the value is changed then it has to be
changed in all the locations in the code where it has been used. Instead if we are using
constants, all we will need to do is to change the value of the constant. This would
propagate the changes to our entire application.
Constants are declared as follows
Const x as Integer
VB.NET supports block-level scoping of variables. That is you can declare and use
variables as and when you need them. Thus, if a variable is required within a ‘for’ block
it can be declared within the block and its scope will be the end of the block.
Simple Types (Primitive Data types)
Simple or value type variables are those, which are assigned space in the stack instead of
the heap. All the primitive types such as int, double etc are value type variables. The
simple types basically consist of Boolean and Numeric types, where Numeric is further
divided into Integral and Floating Point.
The first rule of value types is that they cannot be null. Anytime you declare a variable of
value type, you have allocated the number of bytes associated with that type on the stack
and are working directly with that allocated array of bits. In addition, when you pass a
variable of value type, you are passing that variable’s value and not a reference to the
underlying object.
Object Type
Object type or reference type variables are those, which are allocated storage space in the
heap. Reference type objects can be null. When a reference type is allocated under the
covers a value is allocated on the heap and a reference to that value is returned. There are
basically four reference types: classes, interfaces, delegates and arrays.
Class Type
Custom data types are available in .NET framework in the form of classes or class type. It
is nothing but a set of data and related behavior that is defined by the developer.
Object type and class type are both reference type variables. The only difference comes
from the fact that object type consists of objects predefined and available with the .NET
framework such as string whereas class type consists of custom user defined data types
such as the Class Employee given below.
Class Employee
Dim empid As Integer
Dim empname As String
Public Sub New()
empid = 10
empname = "Reshmi"
End Sub
End Class
name, but with different parameters lists. This is a feature of polymorphism. It is
accomplished by using the Overloads keyword in VB.NET. A simple example would be
an addition function, which will add the numbers if two integer parameters are passed to
it and concatenate the strings if two strings are passed to it.
Class test
Public Overloads Function Add(ByVal x As Integer, ByVal y As
Integer)
Return x + y
End Function
Public Overloads Function Add(ByVal x As String, ByVal y As
String)
Return x & y
End Function
Shared Sub main()
Dim a As new test
Dim b As Integer
Dim c As String
b = a.Add(1, 2)
c = a.Add("Reshmi", " Nair")
System.Console.Writeline(b)
System.Console.Writeline(c)
End Sub
End Class
O/P:
3
Reshmi Nair
Overriding
Class inheritance causes the methods and properties present in the base class also to be
derived into the derived class. There might arise a situation wherein you would like to
change the functionality of an inherited method or property. In such cases we can
override the method or property of the base class. This is another feature of
polymorphism. You can accomplish this in VB.NET by using the Overridable keyword
with the base class method and the Overrides keyword with the derived class method.
Public Class shapes
Public Overridable Sub display()
Console.WriteLine("Shapes")
End Sub
End Class
Public Class square
Inherits shapes
Public Overrides Sub display()
Console.WriteLine("This is a square")
End Sub
End Class
Public Class rectangle
Inherits shapes
Public Overrides Sub display()
Console.WriteLine("This is a rectangle")
End Sub
End Class
The above example is just an indication to how overriding can be implemented in either
VB.NET.
This summary is not available. Please
click here to view the post.
mechanism to read, write, or compute the values of private fields through accessors.
Properties are an extension of fields and are accessed using the same syntax. Unlike
fields, properties do not designate storage locations. Instead, properties have accessors
that read, write, or compute their values.
Get accessor
The execution of the get accessor is equivalent to reading the value of the field.
The following is a get accessor that returns the value of a private field name:
Dim name as String ’ the name field
Property Name() As String ’ the name property
Get
Return name
End Get
End Property
Set accessor
The set accessor is similar to a method that returns void. It uses an implicit parameter
called value, whose type is the type of the property. In the following example, a set
accessor is added to the Name property:
Dim name as String ’ the name field
Property Name() As String ’ the name property
Get
Return name
End Get
Set(ByVal Value As String)
Name = value
End Set
End Property
When you assign a value to the property, the set accessor is invoked with an argument
that provides the new value. For example:
e1.Name = "Reshmi" // The set accessor is invoked here
It is an error to use the implicit parameter name (value) for a local variable declaration in
a set accessor.
How to make a Property Read Only/Write Only
There are times when we may want a property to be read-only – such that it can’t be
changed. This is where read-only properties come into the picture. A Read Only property
is one which includes only the get accessor, no set accessor.
For instance,
Public ReadOnly Property EmpID() as Integer
Get
Return empid
End Get
End Property
Similar to read-only properties there are also situations where we would need something
known as write-only properties. In this case the value can be changed but not retrieved.
To create a write-only property, use the WriteOnly keyword and only implement the set
block in the code as shown in the example below.
Public WriteOnly Property e as string
Set
e = Value
End Set
End Property
members that can be of different data types. It can contain fields, methods, Etc.,
Structures are very similar to classes but there are some restrictions present in the case of
structures that are absent in the case of classes. For example you cannot initialize
structure members. Also you cannot inherit a structure whereas classes can be inherited.
Another important feature of structures differentiating it from classes is that a structure
can't have a default parameter-less constructor or a destructor. A structure is created on
the stack and dies when you reach the closing brace in C# or the End structure in
VB.NET.
But one of the most important differences between structures and classes is that structures
are referenced by value and classes by reference. As a value type, allocated on the stack,
structs provide a significant opportunity to increase program efficiency. Objects on the
stack are faster to allocate and de-allocate. A struct is a good choice for data-bound
objects, which don’t require too much memory. The memory requirements should be
considered based on the fact that the size of memory available on the stack is limited than
the memory available on the heap.
Thus we must use classes in situations where large objects with lots of logic are required.
Struct – Code: Sample code showing the Class vs. Structures
Imports System
Class Test
Dim classvar As Integer
Dim anothervar As Integer = 20
Sub New()
classvar = 28
End Sub
Structure ExampleStruct
Dim i As Integer
Sub New(ByVal j As Integer)
i = j
End Sub
Sub trialMethod()
Console.WriteLine("Inside Trial Method")
End Sub
End Structure
Shared Sub main()
Dim t As New Test()
Dim strct As New ExampleStruct(20)
Console.WriteLine(strct.i)
strct.i = 10
Console.WriteLine(t.classvar)
Console.WriteLine(strct.i)
strct.trialMethod()
End Sub
End Class
O/P: -
28
20
10
Inside Trial Method
In the above example, I have declared and used a constructor with a single parameter for
a structure. Instead if I had tried to use a default parameter-less parameter I would have
got an error. But the same is possible in the case of classes as shown by the default
parameter-less constructor, which initializes the classvar variable to 28.
Another point to note is that a variable called anothervar has been declared and initialized
within the class whereas the same cannot be done for members of a structure.

