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.
Ok in this public class 'Mark', let's talk about what a constructor is. Every class in .NET will have a constructor. Now a constructor is a public sub, and I'm going to put this in here and show it to you. Public sub - 'New' is the name of it. And this is code in this sub that will run every time an object is instantiated from this class. And so just for an example, I'm going to say x = 53 and y = "hello", so now every time anybody instantiates a new object off of this class mark, it will come into being with x being equal to 53 and y being equal to hello. And to test that, we'll come up here in sub-main, this is the beginning point for our program, on a console program, and we will create an instance of this particular class. So I'll say - Dim newobj As New Mark () - and we'll go over this a little bit later, and I'll say - console.WriteLine (newobj. - and notice it's showing us x and we'll choose x here. Ok and so we'll build the project, we build class test, and the name of this project's class test and so I will go out to my command prompt and I will run class test. And you will notice that it's going to return 53. Because all we did with this particular program was write the value of x to the console. And notice when we created our new object, right here off of the class 'mark' here, we automatically in our constructor set x to be 53. Now there's something else that we can do, and that is we can pass this information into the public sub and have our constructor build these dynamically. Now the way we can make this happen dynamically, is I can tell it on new that I'm going to pass in 'a' as an integer, and then I'm going to pass - ByVal b As String. Ok and then down here I'll say whatever I pass in as 'a' set equal to 'x', and whatever I pass in 'b' set equal 'y'. And then when I create my new object as a new Mark, I will tell it to put 42 in there and we'll put "hello there". And so now when I create my new object I'll pass these in and set them equal to that. Ok, so now when I run this, my - console.WriteLine (newobj.x) - should be whatever is passed in as 'a'. And we are going to pass in 42. So I'm going to build my project, build class test, then I'll go out to my command line and I'll run class test. And notice it's returned 42 because that's what I passed in here. Now that's an example of passing parameters to my constructor, and here's my constructor. So a constructor is a great place to set up house-keeping on your objects, anything that needs to be in place. You can use these to establish connections to databases, to set values on variables or strings or whatever. So this is a real quick, real simple illustration of what a constructor is and what you can do with it.
| 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 |