Hitterman
25-10-08, 07:56 PM
This tutorial is taken from famous book CIG VB6.
Declaring Variables
Variables can exist as many different types.Now you can think of variables as holding either numeric values or text. When you wrote the previous program, you didn't tell Visual Basic too much about the variable cars, so Visual Basic waited to see what the program was going to do with it. When the program assigned the TextBox control's text to the variable, Visual Basic made cars into a string variable (one that can hold text) and copied the control's text into the variable.
Technically, only a string variable can hold text and only a numerical variable can hold numerical values. This might not seem to be the case with Visual Basic because Visual Basic is so smart about figuring out how a program uses its variables. However, this cleverness comes with a price: When Visual Basic has to figure out how to use a variable, it takes longer for Visual Basic to assign a value to the variable. For this reason, it's not a bad idea to declare your variables before you use them.
When you declare a variable, you not only tell Visual Basic the variable's name, but also how the variable is to be used. For example, the following is a rewritten version of the previous procedure:
Private Sub Command1_Click() Dim cars As Integer cars = Text1.Text Form1.Print "You have " & cars & " cars." End SubNotice the line Dim cars As Integer. This line tells Visual Basic that the program will use the variable cars to store integer data. (If you remember your math, you know that an integer is any whole number.) Now, when Visual Basic transfers the text from the TextBox control, it changes the text from a string to a numerical value and stuffs the result into cars.
This is all well and good, but what happens if the user types non-numerical characters into the TextBox control? For example, what if the user typed ten into the box instead of 10? There's no way that Visual Basic can change the string ten into a numerical value, so Visual Basic generates an error, which looks like this:
Visual Basic displays errors when something goes wrong in your program.
A type-mismatch error tells you that the program tried to store the wrong type of data into a variable. You can never store a string in a numerical variable. For example, these Visual Basic lines cause an error:
' Dont' do this! Dim value As Integer value = "This is a string literal"You can, however, create a variable for holding text, by declaring the variable's type to be String. Here's a version of the Click procedure that declares cars as a string variable:
Private Sub Command1_Click() Dim cars As String cars = Text1.Text Form1.Print "You have " & cars & " cars." End SubThis version of the procedure works just like the previous one, with one small difference. It doesn't matter whether the user types a string or a number into the text box. In both cases, the result is a string that is stored in cars. You might find it confusing that the value 10 can be both a numerical value and a text value. But you have to remember that there's a big difference between the text character "1" and the value 1. When numbers are part of a string, Visual Basic treats them just the same as any other character, such as "A" or "?". The downside is that a program cannot perform mathematical operations with numbers that are stored as strings. Numbers in a string are not really numbers at all, any more than letters are numbers.
Credits to:Clayton Walnum
Declaring Variables
Variables can exist as many different types.Now you can think of variables as holding either numeric values or text. When you wrote the previous program, you didn't tell Visual Basic too much about the variable cars, so Visual Basic waited to see what the program was going to do with it. When the program assigned the TextBox control's text to the variable, Visual Basic made cars into a string variable (one that can hold text) and copied the control's text into the variable.
Technically, only a string variable can hold text and only a numerical variable can hold numerical values. This might not seem to be the case with Visual Basic because Visual Basic is so smart about figuring out how a program uses its variables. However, this cleverness comes with a price: When Visual Basic has to figure out how to use a variable, it takes longer for Visual Basic to assign a value to the variable. For this reason, it's not a bad idea to declare your variables before you use them.
When you declare a variable, you not only tell Visual Basic the variable's name, but also how the variable is to be used. For example, the following is a rewritten version of the previous procedure:
Private Sub Command1_Click() Dim cars As Integer cars = Text1.Text Form1.Print "You have " & cars & " cars." End SubNotice the line Dim cars As Integer. This line tells Visual Basic that the program will use the variable cars to store integer data. (If you remember your math, you know that an integer is any whole number.) Now, when Visual Basic transfers the text from the TextBox control, it changes the text from a string to a numerical value and stuffs the result into cars.
This is all well and good, but what happens if the user types non-numerical characters into the TextBox control? For example, what if the user typed ten into the box instead of 10? There's no way that Visual Basic can change the string ten into a numerical value, so Visual Basic generates an error, which looks like this:
Visual Basic displays errors when something goes wrong in your program.
A type-mismatch error tells you that the program tried to store the wrong type of data into a variable. You can never store a string in a numerical variable. For example, these Visual Basic lines cause an error:
' Dont' do this! Dim value As Integer value = "This is a string literal"You can, however, create a variable for holding text, by declaring the variable's type to be String. Here's a version of the Click procedure that declares cars as a string variable:
Private Sub Command1_Click() Dim cars As String cars = Text1.Text Form1.Print "You have " & cars & " cars." End SubThis version of the procedure works just like the previous one, with one small difference. It doesn't matter whether the user types a string or a number into the text box. In both cases, the result is a string that is stored in cars. You might find it confusing that the value 10 can be both a numerical value and a text value. But you have to remember that there's a big difference between the text character "1" and the value 1. When numbers are part of a string, Visual Basic treats them just the same as any other character, such as "A" or "?". The downside is that a program cannot perform mathematical operations with numbers that are stored as strings. Numbers in a string are not really numbers at all, any more than letters are numbers.
Credits to:Clayton Walnum