Click Here For A Free vLab!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


There’s a wide range of data types, so we’ll 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.1—those are the suffixes you can add to the end of values or variables to tell Visual Basic their type, like strUserFormatString$.

Table 3.1 Variable types.
Variable Type Bytes Of Storage Literal Suffix Range
Boolean 2 N/A True, False
Byte 1 N/A 0 to 255
Currency 8 @ -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Date 8 #…# 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59
Decimal 12 N/A -79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335
Double 8 # -1.79769313486232E308 to -4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values
Integer 2 % -32,768 to 32,767
Long 4 & -2,147,483,648 to 2,147,483,647
Object 4 N/A N/A
Single 4 ! -3.402823E38 to -1.401298E-45 for negative values and from 1.401298E-45 to 3.402823E38 for positive values
String N/A $ A variable-length string can contain up to approximately 2 billion characters; a fixed-length string can contain 1 to approximately 64K characters
User-defined data type N/A N/A N/A
Variant N/A N/A N/A

As you can see in Table 3.1, Visual Basic has a large number of data formats. The Variant type deserves special mention, because it’s the default variable type. If you don’t 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, however—they 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 Types

Visual Basic supports a number of ways of converting from one type of variable to another—in fact, that’s one of the strengths of the language. The possible conversion statements and procedures appear in Table 3.2.

Table 3.2 Visual Basic data conversion functions.
To Do This Use This
ANSI value to string Chr
String to lowercase or uppercase Format, LCase, UCase
Date to serial number DateSerial, DateValue
Decimal number to other bases Hex, Oct
Number to string Format, Str
One data type to another CBool, CByte, CCur, CDate, CDbl, CDec, CInt, CLng, CSng, CStr, CVar, CVErr, Fix, Int
Date to day, month, weekday, or year Day, Month, Weekday, Year
Time to hour, minute, or second Hour, Minute, Second
String to ASCII value Asc
String to number Val
Time to serial number TimeSerial, TimeValue


TIP:  Note that you can cast variables from one type to another in Visual Basic using the functions CBool(), CByte(), and so on.

Setting Variable Scope

You’ve just finished creating a new dialog box in your greeting card program, and it’s a beauty. However, you realize there’s 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?

It’s 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, you’ll 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 variable—it’s much better to refer to the text in the text box this way (assuming the name of the dialog form you’ve created is Dialog):

intNumberBalloons = Dialog.TextBox1.Text


Previous Table of Contents Next


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.