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.
In the previous video, we talked about the dangers of using global properties. Global properties expose us to a lot of potential problems and in this video we want to talk about using properties the correct way. And with this we are going to use properties kind of as a subroutine, and we are going to set those properties and then hand out those property values through some code. And I'm going to show you how much better this is. First of all this allows us to verify the value of our variables before we allow them into our program. Now this is absolutely necessary, because users have a knack for providing us with exactly the most dangerous things possible. And it's amazing how they do this, they certainly don't intend to, it's just that through a lack of knowing exactly what to put or maybe getting in a hurry, making a mistake, they put information in that can actually alter a program in some pretty serious ways. This allows us to protect our programs. For example I've used it before, a program about a car, we should not allow a car to go 600 miles an hour just because the user fat fingers the keys or puts the wrong thing in because he's not sure what to put in. It also helps us avoid errors. Now the kind of errors it helps us avoid are the most terrifying type errors the developer can encounter. And those are those errors where everything appears to be ok, we don't get exceptions thrown or any kind of errors raised or anything like that, the program returns results, it's just that these results are skewed because the numbers that were passed in our variables were out of whack. And so we are going to check those before we go in. Let's go out to visual studio and I want to show you this. If you remember I'm in that same console application that we are using in our examples. In the public class Mark, we had the global variable x and if you remember anybody could change this. Instantiate an object - boom! - x is there, there is not a lot we could do about that. What I'm going to show you now is how to use properties to set these things. Now notice what I'm going to type - Public Property - I'm going to say y - y as Int16. I think the hardest thing about these videos is typing and talking at the same time. Nevertheless notice that when I press enter, something really kind of cool is going to happen here. When I press enter, you'll notice that VB .NET set up for me the get and set for this public property. There is one other thing that I need to add to this, now this looks familiar to you if you are coming from VB6 because we have these public properties there. But they were in two totally separate sections, and the get and set used to be let but now it's set. Get and set have all been combined into one property. And this has made it much, much better. Now there is one other thing I need to add, and that's something called a field. Ok and a field is a private property and with this I set - Private yy As Int16 - and this is what is going to be used inside the property. And notice it is private which means that people from the outside who have instantiated objects can't see this and can't get to it. So here's what we are going to do and to save time, you don't have to watch me type a bunch of stuff. I'm going to pull up here some things that I wrote a little earlier a to save some typing. Notice I've got my field here and this is a private variable, and then I'm exposing my public property y as integer 16. Now in the get portion, which means that if somebody asks for the value of y, we are going to return to them yy, now why that? OK I think I can make this easier, YYY so forth. But I'll try to make a little plainer. Because when we set the value of this and let's look up here and notice that if I set aa.y = 99, then when I call the property for Y, I'm passing in right here 99 and it jumps down to the set and value then gets passed in, value is equal to 99. And here we check it. If 99 is greater than 75 then write back to the user - "Too fast - must be below 76" - we are not going to let the car go faster than 76 miles an hour. So as you see we are going to check the value of our variables as they come in, now obviously I could write as much code as I want to write in here, and check these variables as many different ways as I want to, to make sure they are in compliance with what my program expects and what will allow my program to run properly. Now if this value is not greater than 75, then we are going to set the value that was passed in equal to yy. And then we're going to tell the people that the car is now going this new value in speed. So notice this would be what they passed in, we've now set yy to that so the next time someone tries to get the value of this they will receive the last thing it was set to. Now this is extremely powerful, as extremely protective of our program. And I want to go up here and show you how this looks. Now if I create an object, if I - Dim aa as a new instance of Mark then you'll notice when I press aa that Y appears as a property. Now x appears as a variable, y appears as a property. And you'll notice the difference here. And I can easily set Y equal to and we'll say 92. Now if I set it equal to 92 and run this program. Notice what's going to happen, I'm going to build my class test then I'm going to go out here and run this thing. So if I type class test at the command prompt, you'll notice that it will come back and tell me that's "Too fast must be below 76". Well, if I go back and if I set yy equal to say 72, that's now below 75 and since this is not going to be true, it will come to here and tell me how fast the car is going. So I'm going to build it again; and I could have written a console write line and asked some questions and saved this. But we won't get into all that. So I'm going to call it again and this time it says 72 and it just tells me that the car is going 72. So you can see it's checking the variables to make sure that they are within a certain range. To make sure they don't blow up my program here and cause problems for me. Now understand also real quickly, I can make these read only or write only, I can make it write only by taking out the get. Which means I can only change the value and adding write only up here in my declaration. I can make it read only by taking out the set portion and typing read only up here in my declaration. And so this is how I handle read and write only properties. So this is the best way to use properties. This is the suggested way that you use these things because obviously you can protect your program from user error.
| 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 |