In VB.NET, you can combine a number of related variables together and treat them as one unit using structures in VB.NET. This simplifies programming and makes updating the source of your applications easier.
Let us first consider the following example; you want to store the information about a person/employee. The information include name, telephone number and salary. So far we learned that to store such information you should define 3 distinct variables:
1 2 3 |
Dim Name As String Dim TEL As String Dim Sal As Decimal |
Later on you fill these variables with values, and uses them. Now what is you have two employees? Obviously you define another 3 variables:
1 2 3 |
Dim Name2 As String Dim TEL2 As String Dim Sal2 As Decimal |
Now what if you have a 1000 employee? Well a better solution is to use arrays.
However now you need to define 3 arrays:
1 2 3 |
Dim Names() As String Dim TELs() As String Dim Sals() As Decimal |
The first array stores the names, the second stores telephone numbers, and last one stores salary. So arrays handles the information for large amount of data pretty well. But what is you need to add another property such as address? The solution is to add another array:
1 |
Dim Address() As String |
And if you need to store another property you need to store another array. If you have 14 property for an employee, then you have to store and manage 14 different arrays. In such situations structures are useful. You define a structure to combine the different properties like this:
1 2 3 4 5 6 |
Structure PersonInfo Dim Name As String Dim Tel As String Dim Sal As Decimal Dim Address As String End Structure |
Now you have a new data type called PersonInfo which contains inside it a name, a telephone, a salary and an address for that particular employee/person.
1 2 3 4 |
Dim A As PersonInfo A.Name = "Roger" A.Tel = "918-89-878" A.Sal = 5000 |
So A here is the name of the variable and it stores all the attributes or properties of employee/person. To access a specific property you use the dot (.) followed by the property. For example A.NAME access the name property of that employee.