Programming:Visual Basic Classic/Data Types
Introduction
Datatypes in Visual Basic can be divided into three groups:
- native
- those types that are understood directly by the Visual Basic compiler without assistance from the programmer,
- user defined
- commonly referred to by the initials UDT, meaning User defined Type, these correspond to Pascal records or C structs,
- classes
- these are the basis for object oriented programming in Visual Basic. Note that forms and the various Add-inn and database designers are also classes.
Built in Types
The built in types are:
- Byte
- eight bit, unsigned
- Integer
- 16 bit, signed
- Long
- 32 bit signed
- Single
- 32 bit floating point, range about
- Double
- 64 bit IEEE floating point, range about
- Currency
- exact representation of decimal numbers of up to four decimal places (is this correct)
- String
- dynamically allocated UniCode strings, theorectical capacity about 29 characters.
- Collection
- an associative array of Variants.
- Object
- a holder for any type of Object.
- Variant
- a holder for any type of value or object.
Byte, Integer & Long
Example:
Dim a as Byte
Dim i as Integer
Dim x,y as Long 'Define two variables. Note that only the last variable will be a long integer.
Now those variables will only be capable of storing integer values (without decimal). Long integers can store a number with a bigger range of value than integers but they occupy a bigger space of RAM.
| Type | Storage | Range of Values
|
| Byte | 1 byte | 0 to 255
|
| Integer | 2 bytes | -32,768 to 32,767
|
| Long | 4 bytes | -2,147,483,648 to 2,147,483,647
|
Some functions you need to know: Int()
Int() converts a decimal value into a integer value:
Dim i as Integer
i=Int(3.9)
Print i
Will print:
3
Single & Double
These data types can store decimal values. "Double" compared to "Single" is similar to the "Long" compared to "Integer":
| Type | Storage | Range of Values
|
| Single | 4 bytes | -3.402823E+38 to -1.401298E-45 for negative values
1.401298E-45 to 3.402823E+38 for positive values.
|
| Double | 8 bytes | -1.79769313486232e+308 to -4.94065645841247E-324 for negative values
4.94065645841247E-324 to 1.79769313486232e+308 for positive values.
|
Some useful functions: Round()
Round() rounds off a decimal to a certain number of decimal digits that the programmer wants. The first argument should be a decimal value which you want to round off. The second argument specifies the number of decimal digits you want, for example:
Dim pi as Double
pi=3.141592653589
pi=Round(pi,2) 'Rounds off 3.141592653589 to only two decimal digits
Print pi
Will print:
3.14
String
A string is an array of characters. As an example:
Dim a As String
a = "This is a string"
Strings can be concatenated (connected together to form a new string) using the "&" operator:
Print "Wiki" & "book" & "s"
Will print:
Wikibooks
A normal string variable occupies 10 bytes plus its own size(the string's size) of RAM and can hold as long as 0 to 2 billion characters!
Some frequently used built-in string constants: vbTab, vbCrLf
vbTab contains a string that does the same thing as the Tab key on your keyboard, while vbCrLf creates a new line(similar to the Enter key):
Print "Jack:" & vbTab & "1 pie" & vbCrLf & "me:" & vbTab & "10 pies"
Will print:
Jack: 1 pie
me: 10 pies
To include special characters and quotation marks in the strings, the Chr() function may be used:
Dim a As String
Print "A quotation mark: [" & Chr(34) & "]"
a = "Replace 'apostrophes' for quotation marks"
Replace( a, "'", Chr(34) )
Print a
Some string functions: Str(),Val(),inStr(),Mid(),Replace(),Trim()
In fact there are tons of built-in string manipulation functions available. But right now, I'll just introduce two: Str() and Val().
Str() converts any numerical value into a string value while Val() converts a string value into a numerical value(only when it's convertable).
Dim MyString As String
Dim MyNumber As Single
MyString=Str(300) 'converts a number into a string
MyNumber=Val("300") 'converts a string into a number
If you don't do the conversion. You'll end up getting Type Mismatch Errors.
| [[../Files|Previous: Files]]
| Contents
| [[../Procedures and Functions|Next: Procedures and Functions]]
|
|