Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,410,166

How to create and use Structures [VB.NET/C#]

Tutorial Name: How to create and use Structures [VB.NET/C#]  

Category: PC Tutorials

Submitted By: Skittle

Date Added:

Comments: 0

Views: 3,040

Related Forum: PC Building Forum

Share:

In this tutorial I will show you how to use Structures in Visual Basic and C#, I will show examples in both languages for those who do not program in one or the other.
What is a Structure?
A Structure is a a user-defined type with a group of variables that can be read or written. You can declare a variable of your structure, which can then hold properties.

How to declare your Structure
C#

public struct personInfo
{
    public string forename;
    public string surname;
    public byte age;
    public uint weight;
    public int money;
    public Color favoriteColour;
}

VB

Public Structure personInfo
    Public forename As String
    Public surname As String
    Public age As Byte
    Public weight As UInteger
    Public money As Integer
    Public favouriteColor As Color
End Structure


Declaring a variable as an instance of your Structure:
C#

personInfo Fred;

VB

Dim Fred As personInfo


Setting values to properties of your variable
Now, with our variable 'Fred' we have all of the properties of our structure, and we can assign values to them:
C#

Fred.forename = "Fred";
Fred.surname = "Smith";
Fred.age = 18;
Fred.weight = 65;
Fred.money = 1545;
Fred.favoriteColour = Color.Blue;   

VB
The code in Visual Basic is the same as above, but without the semi-colons.

Using the values assigned
Now that we have values assigned to our variable, we can now use those to compare with other values. I have created a new example, using an array of the type personInfo, each element of the array will have a full set of properties just like a single variable. Each element will hold values of different people, I have only assigned age data to the array elements as this is all that will be used, but just imagine that each element is a different person. I added a boolean variable called 'allowedToDrive' to my structure and a global string variable 'result'. Here is my example:
C#

personInfo[] people = new personInfo[3];
//setting ages for people
people[0].age = 16;
people[1].age = 17;
people[2].age = 18;

int count = 0;
foreach (personInfo person in people)
{             
    if(person.age >= 17)
    {
        //the person is allowed to drive
        people[count].allowedToDrive = true;
    }
    else
    {
        //the person is not allowed to drive
        people[count].allowedToDrive = false;
    }
    //adds text to the result string saying whether or not the person can drive
    result += string.Format("Can Person {0} Drive: {1} {2}", count, people[count].allowedToDrive, Environment.NewLine);
    count++;
}
//displays the result of whether or not the person can drive or not
MessageBox.Show(result);

VB

Dim people(2) As personInfo
'setting ages for people
people(0).age = 16
people(1).age = 17
people(2).age = 18

Dim count As Integer
For Each Dim person As personInfo In people
    If person.age >= 17 Then
        'the person is allowed to drive
        people(count).allowedToDrive = True
    Else
        'the person is not allowed to drive
        people(count).allowedToDrive = False
    End If
'adds text to the result string saying whether or not the person can drive
    result += String.Format("Can Person {0} Drive: {1} {2}", count,   people(count).allowedToDrive, Environment.NewLine)
count += 1
Next person
'displays the result of whether or not the person can drive or not
MsgBox(result)


If you would like any further help, feel free to PM me and I will assist as best I can

Ratings

Current rating: 5.00 by 3 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"How to create and use Structures [VB.NET/C#]" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.