Home
Username:
Password:
Microsoft Visual Basic .NET Tutorials

Types / Reference




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

The second main category of types in Visual Basic .NET is the reference type. And a reference type stores a reference to the data that's in the variable, and so when we create a reference type variable and usually these are objects, the variable actually stores a reference to where the data actually lives. Now value types that we talked about in the previous video are stored on the stack, on the memory stack. And if you will just think about the memory in your computer that it can get to is in this giant stack, and it's not wobbly like this, excuse my drawing, and so every time we create a variable we simply go to a different place on the stack and store that, and we get back to it really quickly, and we are storing by-value variable back into that. But a reference type is a totally different thing, and I want to give you a little image here. And that is, you hear that things are stored on the heap or the memory heap. And so we store reference data type. The reference basically points to an address. And the address actually points, it's a reference for where the data's actually stored on the heap. And so every time we store more data, it gets stored out here on the heap, and if you just picture this stuff getting stacked up like stones on this heap; now as we build the heap, and you are going to look at this a little bit later on in the course, this heap is subject to garbage collection. And so every so often, this variable is no longer referenced by anything, neither is this one, neither is this one, and the garbage collector will come along and clean these things up and remove them off the heap. And so if you can get that picture in your mind, reference type simply stores a reference to the data in the variable. Now what can happen here is on the heap is, I can create an object, ok, and it's a reference to a particular place on the heap. Then I can create another object, and if I set it equal to this one, then you'll notice that both of these objects point to the same place. So if I make a change in this object, the change also gets made in this object. And that may or may not be what we want, and so sometimes for example with variables, where we are trying to store numbers, we don't want this kind of action, and sometimes with objects we absolutely do want this kind of action, so this is kind of where it comes into play. Now reference types do provide object-oriented behavior. We get, you know, full inheritance and implementation and so forth with our reference types. Now again, I mentioned it a little earlier, reference types are subject to garbage collection. Which means that we don't have to be concerned with managing resources. Garbage collection will automatically take care that for us and remove those things from the heap. However one thing to remember, you know the old saying goes, there is no such thing as a free lunch, and this certainly isn't free. All these reference types are garbage collecting, while the memory resources are maintained for us, keep in mind every time we create a pointer to a reference type, there is a certain amount of overhead that goes into maintaining that and keeping that pointer, because not only are we storing our data on the heap, we are also storing a pointer to the that data. And so this is certainly not free. And so again I want to stress, I mentioned it in the previous video about value types, that most of this is handled for you in the background. You don't choose whether you want a reference type or a value type. You just simply choose types, and then it takes care of that for you. Now in the next set of videos, we are going to actually look at the different Visual Basic data types that are out there. And I will point out these value and reference types as we go through those things. But I just want you to see the various types that are available that you can use and a quick description of what they are.

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