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.
Popular Posts
-
Now I will show you how to retrieve the data posted from a HTML file in a JSP page. Consider an html page that prompts the user to enter his...
-
Accessing the Standard CGI Variables: To build the successful web application, you often need to know a lot about the environment in which ...
-
INTRODUCTION TO 'C': C is a programming language developed at AT & T's Bell laboratories of USA in 1972.it was designed by d...
-
SQL aliases can be used with database tables and with database table columns, depending on task you are performing. SQL column aliases are u...
-
Learning about Events (Continue) Lets program the Form_Load event. "MsgBox" is Visual Basic command that launch a message box. for...
-
Steps to follow: 1. Write Person.java as shown below in Code-11.3.a under polypackage directory. (You are welcome to do this work using eit...
-
The Command Button's KeyPress, KeyDown and KeyUp Events The events that will be mentioned in the following pages are commonly used, and ...
-
The SQL AVG aggregate function selects the average value for certain table column. Have a look at the SQL AVG syntax: SELECT AVG(Column1) FR...
-
package interfaceexercise; // Define an interface with three abstract methods. // Any class that implements this interface has to // impleme...
-
We will use the Customers table to illustrate the SQL LIKE clause usage: FirstName LastName Email DOB Phone John Smith John.Smith@yaho...
No comments:
Post a Comment