|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Theres a wide range of data types, so well use a table here. The Visual Basic variable types appear in Table 3.1 for reference, making selecting the right type a little easier (note that although Visual Basic lists a Decimal variable type, that type is not yet actually supported). We also include the literal suffix symbols for numeric values in Table 3.1those are the suffixes you can add to the end of values or variables to tell Visual Basic their type, like strUserFormatString$.
As you can see in Table 3.1, Visual Basic has a large number of data formats. The Variant type deserves special mention, because its the default variable type. If you dont declare a type for a variable, it is made a variant:
Private Sub Command1_Click()
Dim NumberTrains
...
End Sub
In this case, the variable NumberTrains is a variant, which means it can take any type of data. For example, here we place an integer value into NumberTrains (note that we specify that 5 is an integer by using the percent sign [%] suffix as specified in Table 3.1):
Private Sub Command1_Click()
Dim NumberTrains
NumberTrains = 5%
End Sub
We could have used other data types as well; here, for example, we place a string into NumberTrains:
Private Sub Command1_Click()
Dim NumberTrains
NumberTrains = "Five"
End Sub
And here we use a floating point value (! is the suffix for single values):
Private Sub Command1_Click()
Dim NumberTrains
NumberTrains = 5.00!
End Sub
Be careful of variants, howeverthey waste time because Visual Basic has to translate them into other data types before using them, and they also take up more space than other data types. Converting Between Data TypesVisual Basic supports a number of ways of converting from one type of variable to anotherin fact, thats one of the strengths of the language. The possible conversion statements and procedures appear in Table 3.2.
Setting Variable ScopeYouve just finished creating a new dialog box in your greeting card program, and its a beauty. However, you realize theres a problem: the user enters the new number of balloons to display the greeting card in TextBox1 of the dialog box, but how do you read that value in the rest of the program when the user closes the dialog box? Its tempting to set up a global variable, intNumberBalloons, which you fill in the dialog box when the user clicks on the OK button. That way, youll be able to use that variable in the rest of the program when the dialog box is closed. But in this case, you should resist the temptation to create a global variableits much better to refer to the text in the text box this way (assuming the name of the dialog form youve created is Dialog): intNumberBalloons = Dialog.TextBox1.Text
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |