Exceptions / Validating Data
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 in this video entitled Validating Data I want to cover something that's vitally important that you understand in your applications and I want to kind of give you some quick little ideas about how to take care of this, but any time we ask the user of our programs for data we want to check that data and you want to check it a lot of different ways and let's look at the program that we're running right now. Now this is just a very overly simply little example to help you get your head wrapped around the various aspects of Exceptions in C Sharp but even in this I want you to notice that on the Form I have put a label that just says Please input a number between 1 and 500. Now that looks good, it feels really good as a developer but those of you ladies and gentlemen who have built applications know that there is an alarmingly high percentage of the population who either are not capable of understanding what this means or are not capable of reading it and understanding that it applies to them or this program. Users, when they see a text box, for whatever reason, will put Social Security numbers, names of pets they had when they were children - all kinds of weird stuff, in these text boxes and they will click the button and so there are a couple of things you need to be aware of. Any time you accept data from users just know that this data can cause some serious problems with your program if you don't fix it, if you don't catch it, look at it, determine if it's correct or not and then reject it or fix it or correct it. And so I want to give you just a quick example of some things that you might want to search for and how you might do it. There are a lot of ways to do this and I don't want to get into a programmer's debate about which is the best way - you can build a Class and blah, blah, blah and yes that's the object-oriented way and the best way to go but, at this point in this class I just want to show you kind of a quick and easy way. If this is an out that in this class I'm going to be taking data from a lot of different places the first thing that I want to do is, I want to take whatever the user puts in that Text Box - some will say textBox1.Text. I'm going to capture that and I'm going to pass it down here to this little method, function, subroutine - whatever you want to call this thing Ð and I want to do something with it. So whatever they put in the Text Box the very first thing I'm going to do when they click on the button is I'm going to pass that data down here and I'm going to pass it in. I'm going to take all of this out and I'm going to give you a quick example here of some of the ways I would check this. First of all, I'm going to check to see if x is equal to nothing. If x is equal to nothing it means that they didn't type anything into the Text Box, they just clicked on the button. So what am I going to do? I'm going to throw a new exception that says: Cannot leave text blocks blank. Now you can't use a word like you know, stupid or dummy, or anything like that in your text messages as tempted as you may be and so notice I've got thrown here, so I'll fix that, so throw new exception. So the first thing I checked is, did they even put anything in the text box and if they did we'll throw an exception. The next thing, let's, let's, let's do something here and actually I should have done it right up here, actually I shouldn't have done it right up there. Let's create an integer called b and set it equal to ConvertToInteger x. So we're going to take whatever, now we're checking string issues here - did they put anything in - now and we can do all kinds of stuff here. We could check the length of a string if we were asking for strings but we weren't. First thing I'm going to do is check it for an empty text box and now we're going to say let's convert it to an integer, integer b and now let's do some If statements. For example, If b is less than 1 - well, what are we going to do? We're going to throw a new exception and to save time and keystrokes I'm going to copy and I'm going to paste it right here and I'm going to say that Number must be between 1 and 500, you know and if I really want to expound on that and remind them again or give them a more detailed error message I can do that, right? Then I can come down here and I can do If b is greater than 500, right, now I'm going to throw an exception and I'm going to give them this number again. OK? And, I might want to get a little more detailed with them here: Number, we'll change this, cannot be, cannot be greater than 500. That make sense? And so now all we have to do is run this and so when they click on it, it's going to pass whatever's in that text box down to here, we're going to manipulate it and put all of my conditions in here. Now one thing I like about putting these in one place or separate Class is that all in one place and I can go back and verify them and check them and change them as things happen. So, we run our application and we're getting build errors and what's happened here, I have fat-fingered text again. I have to put a capital T there. Alright? So, I'll run it again. Ah, the beauties of real life. Now, notice I'm not going to put anything in the text box and click it and there's my error down there that says: Cannot leave text box blank. See, because it grabbed this, passed it into here and it saw that it was blank and it threw this exception. Pretty cool stuff? We OK now? What if I put a zero in? It throws the exception. Number must be between 1 and 500. I put a 600 in. Number cannot be greater than 500. So, we always, always grab user's data check it. Check it nine ways to Sunday as the saying goes and make sure that they're not doing something that's going to break or cause problems with your program. So Validating Data - make sure you take care of business there, OK.
Tutorial Information
| Course: | Introduction to Microsoft C# 2008 |
| Author: | Mark Long |
| SKU: | 34046 |
| ISBN: | 1-935320-78-5 |
| Release Date: | 2009-10-09 |
| Duration: | 7 hrs / 76 lessons |
| Work Files: |
Yes |
| 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
United States 