Variables and Constants / Option Explicit
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
In this movie I will discuss an important feature in the Visible Basic environment that's called Option Explicit. Option explicit, by default, should be turned On when you install Visual Basic. You can check this by clicking on Tools and then Options and then going to the Projects and Solutions VB Defaults and you should notice there is a setting for Option Explicit and by default it should be turned on. If for some reason your version of Visual Basic has it set to off, you should turn it on now. Back to the form, let's see how this Option Explicit actually helps you in the code writing process. If I go to the code for this partially completed program, remember; Option Explicit has been turned On so if I were to add a third dim statement such as result integer as integer to my code, you will see immediately I have a green underline and this is really a warning as opposed to an error that it's telling me there's an unused local variable result integer. Well, that is the variable I just created. Since in the process of creating this code I have not actually used result integer, it is simply warning me that I have not used this variable. So if I come down to the bottom part of the code within the add button procedure and I use this variable, such as result integer equals num one integer plus num two integer, after I type that line, you notice the line underneath the dim statement for result integer has now gone away because I've now used that variable. So again, don't be alarmed when you type in your dim statements and you get these underlined warnings that says they have not been used. That's really simply a helpful message that says you have the variable, eventually you should use it. Now, that's part of Option Explicit, but not really the main part. The main benefit of using Option Explicit is that if you are to misspell a variable, it is going to force you to be consistent with that spelling. By that I mean this: let's go back to our dim statement for result integer and I will simply misspell result by leaving out the t in result. Now, as soon as I do that, notice that the dim statement, which actually is where the error has occurred, does not have a problem, does not have a line underneath of it. The problem is resulting down in the last statement where it now tells me that result integer is not declared. That is because I did not spell it correctly up here as we know. So this is simply an immediate and notice the underline is blue, which means it has to be addressed at this point. So this giving you immediate feedback that the variable that I am trying to use is not declared. Even though the problem is in the dim statement, it's not showing up until down here where I'm actually using it. Now, let's note what happens if the Option Explicit feature is not turned On. And one of the ways to turn off the Option Explicit feature is by simply typing option explicit off at the very top of your code. This is even above the public class statement. Now, as soon as I do that, notice that the offending line has now, the error message has gone away and we're now back to the unused local variable, which is the misspelled result integer variable. So what Option Explicit off is really saying is it's OK to use that variable but ultimately it's going to create problems for you. But this is not going to prevent me from compiling and executing this program, although it may end up with incorrect results in my result integer. In other words, by using Option Explicit on, which is the default, when you are declaring your variable, Visual Basic is going to make sure that that variable, the spelling of that same variable is used throughout the program. If you do not use the correct spelling of that variable, then it's going to adjust and perhaps lead to incorrect results. You want your dim statements to be the key; that is the way you spell the variables in the dim statement is how they should be spelled throughout, as opposed to allowing the misspelled word in the dim statement to be accepted and then use the correct version later on. This is going to lead to all types of inconsistencies and confusion when you try to debug the program. So this is really a feature that you should never have in your program, Option Explicit Off. It is a feature that's available in Visual Basic but it's not one that I suggest that you use. So again, leave the feature Option Explicit turned On.
Tutorial Information
| Course: | Microsoft Visual Basic 2005 |
| Author: | Arthur Lee |
| SKU: | 33940 |
| ISBN: | 1-935320-10-6 |
| Release Date: | 2008-11-19 |
| Duration: | 7.5 hrs / 97 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 