Home
Username:
Password:
Microsoft Visual Basic .NET Tutorials

Structures / Creating a Structure




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Now that we've talked about what a structure is in a previous video, let's go take a look at creating a structure. It's a very short video coz there is just not a lot to this; let's go out to our console application and take a look at creating a structure. First thing I want to do is go outside my main Sub and I'm just going to type, get my Caps right, Structure Mark. Notice it puts a squiggly under Mark; if I mouse over, it tells me it has to have at least one instance member variable or event, and so I'm going to say - Public a As integer - then I will do a - Public Sub DoThis() - and normally code would go there. And then I will do a Public Function, DoThat and code will go here. Now notice here's my structure, pretty straightforward, I've got a property, A, that's an integer and then I've got a Sub DoThis and a Function DoThat. Now notice up here to use this, I instantiate an object with it. Get this where you can see it so I'm going to - Dim x as New - and you'll notice Mark shows up, right here it is. And then when I call x, you'll notice there is my property, there is DoThis and there is DoThat. Now equals, get, has, go, get type, to string are being inherited from the base class, which is system.value. And also from system object where everything is ultimately deriving from. But notice I can set A equal to 5, and everything's cool with that. So that's what a structure looks like, that's how to use it. Now, I want to revert back to the example I've shown you in the previous video. Because this structure is going to act, it is a value type. It is not a reference type. So if I was to create, even though I created - Dim y as Mark - and set that equal to x. OK - y.a = 4 - keep in mind, those will still maintain their uniqueness coz these are value types, they weren't placed on the managed heap, they are not a reference, so it actually sees two different variables here. Now there is a question here though. With the managed heap, garbage collection takes care of getting rid of those when we no longer use them; what happens to these types? Well they are just sitting there on the stack, and when the variables go out of scope then those variables simply disappear. Now they are reclaimed. So that's what a structure looks like, that's how you use the structure, you actually instantiate an object to use it, and obviously just like a class we can instantiate multiple objects on this. I could have just as easily done this without any tricks, and then y would still be correct. And I could Dim another one. Dim C as new Mark and then C and notice, so that's a structure now. Structures are pretty cool; basically you want to use structures just like you would a variable when you need to bring this stuff into being and then as soon as it goes out of scope it goes away. And so there are less permanent than classes is probably a good way to think about them. You owe it to yourself to go do some reading on structures to see when you use them; generally what most people find that if they've added very many Subs, or Functions to a structure it probably should have been a class. But again that's a very general term and you know how general terms go. As soon as you make it, somebody comes up with a perfect reason to use a structure; so again this is something else to put out there in that proverbial tool box, pull it out when you need it for that certain job.

Tutorial Information

Course: Microsoft Visual Basic .NET
Author: Mark Long
SKU: 33433
ISBN: 1932072349
Release Date: 2003-05-27
Duration: 6 hrs / 87 lessons
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available