We will be undergoing scheduled maintenance on May 20th, 2013 at 02:00 GMT.
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.
One of the new terms that's getting a lot of attention in Visual Basic .NET is the term structure. Now we didn't have structures in VB6 and this thing is really kind of interesting. And it gets back to the heart of what we've talked about earlier in the course, and that is the difference between value types and reference types. But first of all what exactly is a structure; well a structure is very similar to what we saw in VB6, our user defined data types. A structure is really you can call it a class lite, you know how you've got everything in the world's light now. You have these you have product A, its like less calories, half the fat and so forth and so it's whatever product A lite. But these are a lot like classes except they don't have quite the overhead. And here's the main thing to remember about a structure; it acts like a class but it's value typed, it's not reference typed. And I'm going to show you a good example of that in just a few minutes with some code. Now structures though can utilize variables, we can use sub routines in our structures and we can use functions. And so we set these things up much like a class and then we instantiate them much like a class. Instantiate an object from them and then use the methods and properties just like a class. But you are going to see that these things are doing something very different under the hood in the way that they are being handled with memory. Let's go out to visual studio and take a look at one of these. Ok, here we are in visual studio, and I want to show you this little exercise that I have built. Now this is a console application, and in the first example I have commented out a lot of things here, but in the first example we are going to look at the value types. And I want you to notice what we've done here. Since we are using the integer and that is a value type, that's what's going to make it perform in the way it's going to perform. So I didn't, notice, I didn't code anything here to tell it to use value types, I just used integer 16 and all of my primitive data types are values. So I'm Dimming A as Int16, which is a value type, and setting it equal to 5. Ok then I'm deeming B as Int16 and setting it equal to A, then I come down here and say A =10, now think with me just a minute. We set A was =5, and then we set B=A, then we set A=10. Ok so we are going to write A and then we are going to write B, and see if the values match or if they are different. So I'm going to build a console application3, I should have named it something shorter, I guess. Then I'm going to go out here and run console application3 and you'll notice A is 10 and B is still 5. Now wait a minute, how can B even though I set it equal to A, still be 5? Because these are value types. They are pointing to their particular location in memory, they are not pointing to a common referenced point. Now to show you how they would work if they were referenced, let's take a look at the reference types. And I'm going to uncomment this code and talk to you about this. Now notice in the reference type, I - Dim x as New Mark - and notice there is a class Mark down here with simply a Public aa integer, a variable. So I Dim x as a New Mark, I Dim xx as Mark and set it equal to x. So I set it equal to this object. So what I've got is, both of these objects are pointing to the same place on the managed heap. So I've got two different variable objects pointing to the same reference on the heap, and object x I set aa=3 and then I will do a console.WriteLine aa and xx.aa and see what those values are. Then I will simply set the xx object = 5. Ok and then we will see how this affects both of them. OK in our printout now we've got some text, and text and these to help us understand them. So keep in mind x.aa was =3 and then we changed xx.aa =5. And so I'm going to build this application and I am going to go out and run it again. Now notice something that's interesting here. Whenever x.aa is 3 then xx.aa is 3, and whenever this object sets the variable to 5 the other object's variable becomes 5. And again notice up here A is 10, B is 5, they are pointing to two different values. They just use values on those variables and here everybody is pointing to the same object, so even if one changes the other one sees that change. So that's the main difference between reference and values. Now again this takes us back to where we started the video, we are going to talk about structures that are value types. So structures will behave this way with shared variables and references will act this way. Ok, and so this will allow structures to let you do some things, that reference types won't let you do. Now in the next video, we are going to show you how to create a structure and then after that we'll talk about how to use one.
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |