Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Knowledge Base > Tutors' Corner > Basic Structures in VB.Net


Reply
 
Thread Tools Display Modes
  #1  
Old 03-19-2004, 11:46 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,745
Default Basic Structures in VB.Net


Introduction
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.



Structure Definition
Code:
[attributes] [access level] Structure identifier
    members
End Structure
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
Code:
Private Structure tUsers
    Dim sFirstName As String
    Dim sLastName As String
End Structure
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.
Code:
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:
Code:
'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:
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
    'Access the sNames structure
    MessageBox.Show("From Form: " & sNames(0).sFirstName & " " & _
        sNames(0).sLastName)

    'Access it from a new form
    Dim f As New Form1()
    MessageBox.Show("From f: " & f.sNames(0).sFirstName & " " & _
        f.sNames(0).sLastName)
End Sub
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:
Code:
Private Structure tUsers
    Dim sFirstName As String = "John"
    Dim sLastName As String = "Doe"
End Structure


Final Notes on Structures
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.
Attached Files
File Type: zip Structures_VBNet.zip (5.5 KB, 97 views)
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VB6 vs VB.NET geo1st487 Tech Discussions 10 01-31-2004 10:37 PM
Open VB.NET 2002 project within VB.NET 2003 geo1st487 .NET General 2 01-10-2004 05:13 PM
VB.Net Conversion of BMP to AVI FirebirdGuy Interface and Graphics 1 08-21-2003 03:15 PM
Basic Printing in VB.NET chengcsw General 1 08-07-2003 11:46 AM
A VB6 to VB.NET conversion sample Helmar General 0 11-14-2001 06:27 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->