BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

     BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List"Micro-News"BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List
        Micro-Mailing-List

Understanding and Using Visual Basic Part 1
By: Jared Hoylman -

Being a VB programmer there are many things that I have picked up over the past few years that greatly improved my programs and programming ability. In this series of articles I am going to cover some of the basics of VB programming and some Tips and Tricks to ease you along your way. This series of articles will start with the basic skills needed and work it's way up to the more advanced topics such as sending and receiving data from a Basic Stamp or Microchip PIC..!

Option Explicit
I am sure many of you have seen the words Option Explicit at the top of some VB code before. Why is it there, and what does it do..? Well, the Option Explicit statement forces you to declare your variables before you use them. Whoop-t-do, right..? Wrong..! These two simple word can save you hours of headaches debugging your programs..! It can also speed up your program considerably if used right..!

By placing Option Explicit at the top of every code module before any procedures you can guarantee that you will not misspell any variables. Lets see an example...

Private Sub Command1_Click()
Dim sMississippi As String
sMississipi = "Hello"  '<-- Note the missing "p"
MsgBox sMississippi
End Sub

What this code is actually supposed to do is display a MessageBox with the greeting "Hello". Since the variable is misspelled and there is no Option Explicit at the top of the code module, you get a blank MessageBox..!

Now go to the very top of the code module and type the words Option Explicit. Run the program again. What happened..? You get a "Variable not defined" error. This is a simple fix for what could be a complex problem.

Another reason that Option Explicit is so important is because if you do not declare your variables as a specific data type, VB defaults the variable to being type Variant (See data types explained in the next article). A Variant type variable can hold any kind of data from strings, to integers, to long integers, to dates, to currency, etc. Even though this may sound like the best kind of variable to use, it is not. It is the slowest type of variable..! By defining your variables specifically for the kind of values that will be stored in them, will greatly increase your programs performance.

And to make it even easier, how about if I show you how to make VB automatically add Option Explicit to every code module..! It's easy.

Click on the Tools menu and select Options... Now check Require Variable Declaration Click OK

Now every time you open a new code module the words Option Explicit automatically appear at the top..!

| Intro |  Data Types >>

 

Copyright © 1999-2007
Reynolds Electronics

| Contact Information |