Whos Very Able?
Get it? Very Able? Variable? Hahaha! Oh gawsh, I'm so funny. Please, stop laughing. No autographs, thanks. Sheesh, I should be working on Comedy Central or something.
What are variables, and how can they be used in your Visual Basic apps?
Anyway, I'm not, so back to the content.
Variables can be thought of as invisible Text Boxes, the values of which can be changed with a single line of code. They're just something you hold information in.
For example, let's pretend you wanted to display a number every time the user clicks a Command Button. That's pretty easy. But what if we wanted to automatically increment that number with each click? Hmm, that's where variables can help out. Let's code!
- Launch Visual Basic and create a new Standard Exe
- Add a Command Button to your Form
- Change the button's Name property to "cmdVariableCount" and its Caption property to "Click Me Quick!"
- Double click on a blank area of the Form to open the code window
Near the top of the code window, you'll notice two drop-down boxes. The first specifies an object, the second a procedure. In English, these are defined as:
Object - the thing you want to work with, say cmdVariableCount
Procedure - denotes the 'event' for which you want to write code. For instance, we may write code in the Click event to have it respond when the user clicks on cmdVariableCount. Other cmdVariableCount events include GotFocus and MouseMove. The Procedure drop-down can also display user-defined Subs and Functions, which we'll cover in a later tutorial
In two twitches of a ducks whisker (Ed: In English we say "In a minute") we'll be entering code that responds to the Click event of cmdVariableCount. But first, I'd like to introduce you to a special bit of the code window.
- Click on the object drop-down box, the first one
You should see a list of objects - probably Form, cmdVariableCount and (General). The first two are objects - and you can add code to respond to their events. For instance, you may make a message box appear when someone clicks on cmdVariableCount. Or maybe play a tune when someone opens the form. Or whatever.
But you're probably scratching your head over the latter item, (General). If so, don't - firstly, because I'm about to explain it and secondly, 'cause knits don't appreciate such random attacks.
(Ed: Oh boy, hits have just fallen by 50%!!)
(Karl: And if you were scratching your head, 50% of knits will have just fallen, too!)
- Select the (General) section from the drop-down list
The right-hand combo box should change to (Declarations).
You'll notice that your cursor isn't surrounded by any code as with usual event procedures, such as "Public Sub cmdVariableCount_Click()". That's because you've reached a special place in your code window - the top - where you can create, or "declare" variables.
To declare a variable you use the syntax:
Dim MyVariable as DataType
...where MyVariable is the name of your variable and DataType is the type of information it should hold. Don't forget that a variable is just a non-visible holding place for a value. Here's a short list of the most common DataType values:
String - If you're going to store text in the variable
Integer - Used when storing a number
Boolean - Used when storing a True/False value
Date - Used when story a date value
That's enough theory. Let's declare our own variable in code!
Page 2 of 5