Using Structure and Arrays within Structures

Introduction

This FAQ demonstrates structures by building a Person structure. Some of you that used Types in previous versions of Visual Basic will recognise a structure as they are similar. But in all honesty, .NET structures are more like classes. For example, Structures can have properties (Get and Set) and also methods (Sub and Functions) as you will see in this Person structure.

Code

First of all, you need to define your structure. You can do this at the top of your form code or in a module. The following code defines the Person structure. Add this to the top of your form code (below the statement “Inherits System.Windows.Forms.Form”).



    'Create the Person Structure
    Private Structure Person
        Public FirstName As String
        Public Surname As String
        Public HairColour As String
        Public DateOfBirth As Date

        Public Function Age() As Integer

            Return DateDiff(DateInterval.Year, DateOfBirth, Today)

        End Function

    End Structure

At the moment, this is no good to us. We need to declare a variable of type Person in order to use this structure. This can be done like so:



'Just an example
Dim x As Person

However, for the purpose of this FAQ we are going to be creating an array of the Person structure. Add the following below the structure definition.



    'Declare an array of the Person structure with 3 elements (remember, arrays are zero based)
    Private People(2) As Person

In order to make any use of this Person Structure we will want to populate it with some data. For simplicity I have hardcoded the values to demonstrate this. Add the following to your Form_Load event:



        'Populate the People array with some data
        With People(0)        'Person 1
            .FirstName = "Joe"
            .Surname = "Bloggs"
            .DateOfBirth = #1/1/2003#
            .HairColour = "Brown"
        End With

        With People(1)        'Person 2
            .FirstName = "Jane"
            .Surname = "Docherty"
            .DateOfBirth = #5/25/1978#
            .HairColour = "Blonde"
        End With

        With People(2)        'Person 3
            .FirstName = "Harry"
            .Surname = "Smith"
            .DateOfBirth = #7/28/1964#
            .HairColour = "Black"
        End With

Now that the structure has been populated with data, we will want to read it back. Add a button to your form and add the following code to the Click Event.




        Dim i As Integer
        Dim strPersonDetails As String

        'Loop around the People array and retrieve the details of the person.
        For i = 0 To People.Length - 1

            With People(i)

                strPersonDetails = "Name: " & .FirstName & " " & .Surname & vbCrLf
                strPersonDetails &= "Hair Colour: " & .HairColour & vbCrLf
                strPersonDetails &= "Date Of Birth: " & .DateOfBirth.ToShortDateString & vbCrLf

                'Return the age of the person by using the Age function that is declared within the structure
                strPersonDetails &= "Age: " & .Age

            End With

            MessageBox.Show(strPersonDetails)

        Next

You will notice in the last piece of code that a call to the People(i).Age function is made. This is a function that is defined within the structure that simply returns the persons age using the DateDiff function (this may not be a true age as I only wanted to demonstrate the use of a function within the structure) big grin .

If you run the program now and press the button, you will see a message box showing the details of each of the people stored within the Person Structure.

Conclusion
As you can see from the above, using structures is fairly straightforward and a great way of storing related information.