|
OOP with VB
Structures
Structures can be defined as a tool for handling a group of logically related data
items. They are user-defined and provide a method for packing together data of different
types. Structures are very similar to Classes. Like Classes, they too can contain
members such as fields and methods. The main difference between classes and structures
is, classes are reference types and structures are value types.
In practical terms, structures are used for smaller lightweight objects that do not
persist for long and classes are used for larger objects that are expected to exist
in memory for long periods. We declare a structure in Visual Basic .NET with the Structure keyword.
Value Types and Reference Types
Value Types and Reference Types belong to Application data memory and the difference
between them is the way variable data is accessed. We will have a brief overview about
them.
Value Types
In VB .NET we use the Dim statement to create a variable that represents a value type.
For example, we declare a integer variable with the following statement: Dim
x as Integer. The statement tells the run time to allocate the appropriate
amount of memory to hold an integer variable. The statement creates a variable but
does not assign a value to it. We assign a value to that variable like this: x=55.
When a variable of value type goes out of scope, it is destroyed and it's memory is
reclaimed.
Reference Types
Creating a variable of reference type is a two-step process, declare and instantiate.
The first step is to declare a variable as that type. For example, the following statement Dim
Form1 as new System.Windows.Forms.Form tells the run time to set enough memory
to hold a Form variable. The second step, instantiation, creates the object. It looks
like this in code: Form1=New System.Windows.Forms.Form. A variable
of reference type exists in two memory locations and that's why when that variable
goes out of scope, the reference to that object is destroyed but the object itself
is not destroyed. If any other references to that object exist, the object remains
intact. If no references exist to that object then it is subject to garbage collection.
Code for Creating a Structure
The following code creates a Structure named Employee with five fields of different
data types.
Module Module1
Structure Employee
'declaring a structure named Employee
Dim EmpName As String
Dim EmpDesignation As String
Dim EmpCity As String
Dim EmpSal As Double
Dim EmpId As Integer
'declaring five fields of different data types in the structure
End Structure
Sub Main()
Dim san As New Employee()
'creating an instance of Employee
san.EmpName = "Sandeep"
san.EmpDesignation = "Software Developer"
san.EmpCity = "Sydney"
san.EmpSal = 60000
san.EmpId = 2707
'assigning values to member variables
WriteLine("EmpName" + " " + san.EmpName)
WriteLine("EmpDesignation" + " " + san.EmpDesignation)
WriteLine("EmpCity" + " " + san.EmpCity)
WriteLine("EmpSalary" + " " + san.EmpSal.ToString)
WriteLine("EmpID" + " " + san.EmpId.ToString)
'accessing member variables with the period/dot operator
Read()
End Sub
End Module
|
The output of above code is the image below.
|