What are structures? In VB6 you might call them User-Defined Types. But in .Net, they are more than just a Type. They aren't considered a 'heavyweight' user-defined type like a class is, but instead, they are more 'lightweight'. This means that they will use fewer system resources (read: take up less memory) than their class counterparts. The only drawback is that they provide less functionality than classes.
Structures are similar to classes in that they both can have properties, methods, constructors, nested types, fields, and operators.
However, there are two important differences between structures and classes. One is that structures don't support inheritance and the other is that structures are a value type, and not a reference type.
When to Use Structures
The only time you should use a structure is for types that are small and simple, and are similar in their behavior to "native" types. For example, instead of creating a class to store a user's first and last name or using two arrays of a string, you can use a structure.
[attributes] [access level] Structure identifier
Attributes are a complex concept, so, if you are truly interested, here
is the MSDN link to Global Attributes. The access level is like the VB6 counterpart, you can choose from Private, Public, etc. Identifier is the name of your structure, and the members are the contents.
Creating a Simple Structure
Private Structure tUsers
Dim sFirstName As String
Dim sLastName As String
We have now just made our first structure! However, we can't do anything with it. If you try to access tUsers.sFirstName, you will get an error. Just like any other object, we need to use the New keyword to create a new tUsers class.
Dim sNames(2) As New tUsers
We now have an array with 3 members, each with a first and last name. Now, how do we set these?
Filling the Structure
Well, we could fill the structure in the Form_Load event. But what if someone declares a new instance of our form, but doesn't want to show it to access the variables? We need to set the values in the New() sub. Open up the region called "Windows Form Designer generated code".
In the New() sub, underneath the comment that says "Add any initialization after the InitializeComponent() call", add:
'Add any initialization after the InitializeComponent() call
sNames(0).sFirstName = "John"
sNames(0).sLastName = "Doe"
sNames(1).sFirstName = "Jane"
sNames(1).sLastName = "Doe"
sNames(2).sFirstName = "Jim"
sNames(2).sFirstName = "Doe"
Now, we can access the filled components anywhere in the class. To test this newfound access, let's use the following code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
'Access the sNames structure
MessageBox.Show("From Form: " & sNames(0).sFirstName & " " & _
'Access it from a new form
Dim f As New Form1()
MessageBox.Show("From f: " & f.sNames(0).sFirstName & " " & _
See, we can get the same information from either our form, or a new instance by loading the Structure in the New sub.
No Initialization in Structures
Some of the most common mistakes I know I'm guilty of when working with structures is forgetting that structures cannot be initialized. In other words, you can't do this:
Final Notes on Structures
Private Structure tUsers
Dim sFirstName As String = "John"
Dim sLastName As String = "Doe"
If you consider a structure as its own data type, you can accomplish many things without having numerous variables. And, you can re-use the type. If you use the tUsers structure I mentioned earlier, you can have an array for Customers, and an array for Employees.
Also, as I mentioned earlier, structures can have properties. Check out the Properties tutorial by excaliber here
. Also, if you are interested in more information on Structures, check out the following MSDN articles:
If there are any issues that you come across when using this tutorial, you can contact me through the forum's PM system, the IRC server, and MSN. Hopefully, you have a basic idea now how structures work in VB.Net.