Structures in VB.NET

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:

Later on you fill these variables with values, and uses them. Now what is you have two employees? Obviously you define another 3 variables:

Now what if you have a 1000 employee? Well a better solution is to use arrays.
However now you need to define 3 arrays:

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:

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:

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *