JMA201-01 |
||||
|
Now, we need to talk about variables. A variable is just a named piece of memory, where we can put some data. Before we create our variable, we should consider what kind of data we can have.
We can have different data types in VB. VB allows us to switch between types easily. We sometimes refer to basic categories of data types with the "generic" names below:
As we begin to work with VB, we will need to understand some of the data types we have available to use. Different types reserve different amounts of memory for themselves; the general strategy is to use the smallest data type that still supports the range of values we need.
To declare variables, we will use the keyword Dim and provide the data type, as in the following line:
dim myVar as Stringor perhaps
dim myVar1, MyVar2, myVar3 as string
Strings, when represented directly in code, are placed within quotes. This tells the program that this is a text data value (a string literal), and not the name of a variable.
We can also assign a value to a variable when we declare it:
dim myVar1 as string="Hello!"
Option Explicit
There are 2 very important "flags" we can add to our program that will reduce errors. The first is the Option Explicit tag. Consider the following situation: You creat a variable called userText. You attempt to add something to it in code, but you mistype - you use the name usersText, like this:
Dim userText as string usersText="Hey!" msgbox(userText)
In this situation, the computer would assume usersText was a new variable you wanted to create. It would do so, and then assign it a value. However, the programmer then works with userText, and the msgbox would show nothing. This is known as implicit variable creation (dim automatically when used) and is an easy way to make mistakes.
Option Explicit will force the user to explicity define any variable they want to use. This way, when you attempt to use a variable you haven't dimmed (probably a typo) VB will warn you as you code. Option explicit is one of the few statements that are allowed to be used outside a subroutine - here's an example of it's use:
This demonstrates:
You should always use option explicit - it reduces typo errors!
Option Strict
Option strict serves a similar role. Imagine you're working with integers and doubles. If you don't use option strict, VB will do what is called implicit type conversion, which can cause problems. Consider this code:
dim gpa as integer gpa=3.88 If gpa = 4 Then msgbox("Perfect!") End If if gpa>3.5 then msgbox("Highest Honors!") end if if gpa>3 and gpa<=3.5 then msgbox("Honors!") end if
You might expect it to show the user "Higest Honors!" because the gpa value is 3.88. However - gpa was mistakenly created as an integer. If I try to put a double value into an integer, VB will round to 4 (implicit conversion) and you'll get weird behavior - in this case, both the "Perfect" AND the "Higest Honors" will show.
So, option strict turns off this behavior. If I made this mistake with option strict on, I'd get this message:
If strict is on, you are forced to do conversions explicitly, with the cint, cdec, cdbl etc. commands discussed below. This forces us to consider all data types, and prevents implicit conversion errors.
Available Types
The original articles on MSDN are available:
Data Types: http://msdn2.microsoft.com/en-us/library/47zceaw7(VS.80).aspx
Type Conversion: http://msdn2.microsoft.com/en-us/library/s2dy91zy(VS.80).aspx
Visual Basic type | Memory Needed | Value range |
---|---|---|
Boolean | Depends on implementing platform | True or False |
Byte | 1 byte | 0 through 255 (unsigned) |
Char (single character) | 2 bytes | 0 through 65535 (unsigned) |
Date | 8 bytes | 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999 |
Decimal | 16 bytes | 0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9...E+28) † with no decimal point; 0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest nonzero number is +/-0.0000000000000000000000000001 (+/-1E-28) † |
Double (double-precision floating-point) | 8 bytes | -1.79769313486231570E+308 through -4.94065645841246544E-324 † for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 † for positive values |
Integer | 4 bytes | -2,147,483,648 through 2,147,483,647 (signed) |
Long (long integer) | 8 bytes | -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 (9.2...E+18 †) (signed) |
Object | 4 bytes on 32-bit platform 8 bytes on 64-bit platform | Any type can be stored in a variable of type Object |
SByte | 1 byte | -128 through 127 (signed) |
Short (short integer) | 2 bytes | -32,768 through 32,767 (signed) |
Single (single-precision floating-point) | 4 bytes | -3.4028235E+38 through -1.401298E-45 † for negative values; 1.401298E-45 through 3.4028235E+38 † for positive values |
String (variable-length) | Depends on implementing platform | 0 to approximately 2 billion Unicode characters |
UInteger | 4 bytes | 0 through 4,294,967,295 (unsigned) |
ULong | 8 bytes | 0 through 18,446,744,073,709,551,615 (1.8...E+19 †) (unsigned) |
User-Defined (structure) | Depends on implementing platform | Each member of the structure has a range determined by its data type and independent of the ranges of the other members |
UShort | 2 bytes | 0 through 65,535 (unsigned) |
Conversion Functions
Function name | Return data type | Range for expression argument |
---|---|---|
CBool | Boolean Data Type (Visual Basic) | Any valid Char or String or numeric expression. |
CByte | Byte Data Type (Visual Basic) | 0 through 255 (unsigned); fractional parts are rounded.1 |
CChar | Char Data Type (Visual Basic) | Any valid Char or String expression; only first character of a String is converted; value can be 0 through 65535 (unsigned). |
CDate | Date Data Type (Visual Basic) | Any valid representation of a date and time. |
CDbl | Double Data Type (Visual Basic) | -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values. |
CDec | Decimal Data Type (Visual Basic) | +/-79,228,162,514,264,337,593,543,950,335 for zero-scaled numbers, that is, numbers with no decimal places. For numbers with 28 decimal places, the range is +/-7.9228162514264337593543950335. The smallest possible non-zero number is 0.0000000000000000000000000001 (+/-1E-28). |
CInt | Integer Data Type (Visual Basic) | -2,147,483,648 through 2,147,483,647; fractional parts are rounded.1 |
CLng | Long Data Type (Visual Basic) | -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807; fractional parts are rounded.1 |
CObj | Object Data Type | Any valid expression. |
CSByte | SByte Data Type (Visual Basic) | -128 through 127; fractional parts are rounded.1 |
CShort | Short Data Type (Visual Basic) | -32,768 through 32,767; fractional parts are rounded.1 |
CSng | Single Data Type (Visual Basic) | -3.402823E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.402823E+38 for positive values. |
CStr | String Data Type (Visual Basic) | Returns for CStr depend on the expression argument. See Return Values for the CStr Function. |
CUInt | UInteger Data Type | 0 through 4,294,967,295 (unsigned); fractional parts are rounded.1 |
CULng | ULong Data Type (Visual Basic) | 0 through 18,446,744,073,709,551,615 (unsigned); fractional parts are rounded.1 |
CUShort | UShort Data Type (Visual Basic) | 0 through 65,535 (unsigned); fractional parts are rounded.1 |
val | Numeric | Converts to a number if possible; if not, returns 0. |