Tutorials Navigation
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,051
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#
VB
Declaring a variable as an instance of your Structure:
C#
VB
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#
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#
VB
If you would like any further help, feel free to PM me and I will assist as best I can
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
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,709)
- 02. How to: Matrix Numbers | Batch File(1,964)
- 03. How to Password Protect Files on Windows(874)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,937)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,282)
- 06. How to embed an image on TheTechGame(3,151)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,104)
- 08. Host bot lobbies! Full Tutorial!(11,675)
- 09. Unban yourself [Plutonium BO2](14,328)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,422)
- 11. Best Crosshair Settings for Valorant(6,565)
- 12. Othercide The Surgeon Boss Guide(2,577)
- 13. Othercide Remembrances Unlock Guide(4,529)
- 14. Othercide Beginners Tips and Tricks(2,746)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,899)
"How to create and use Structures [VB.NET/C#]" :: Login/Create an Account :: 0 comments