Object Variables VB Help

An object variable refers to one of Visual Basic’s many objects, and you can use an object variable to access the actual object. You will see examples of object variables in Parts IV and V of the book

Here is a simple example of an-object variable. A Form has two Command buttons on it, Command 1 and Command 2. You can declare two object variables as:

Dim a As CommandButton, b As CommandButton

Each of these two object variables can be set to either one of the Command buttons with the following statements:

Set a = Command 1
Set b = Command 2

From now on, you can manipulate the two Command buttons properties through the variables a and b. To change the Caption property of the first Command:button, use a statement such as the following:

a.Caption = “Hi”

To turn on the bold attribute of the second Command button (so that its caption appears in bold), use the following statement:

b.FontBold = True

You will find more examples of object variables in later chapters. You will also learn how to create your own objects that have their own properties and methods.

Variant Variables

This is the most flexible data type because-it can accommodate all other types, A variable declared as variant (or a’ variable that hasn’t been declared at all) is handled by Visual Basic according to the variable’s current contents. If you a sign an integer value to a variant, Visual Basic treats it as an ·integer. If you assign a string to a variant, Visual Basic treats it as a string. Variants can also hold different data types in the course f the same program. Visual Basic performs the necessary conversions for you .

To declare a variant, use the Dim statement without specifying a type, as follows:

Dim myVar

You can also specify the Variant type to make the code cleaner:

Dim myVar As Variant

however, it isn’t necessary. Every time your code references a new variable, Visual Basic will create a variant for it. For example, if the variable valid key hasn’t been declared, when Visual Basic runs into the following line:

validKey K ‘002-6~bbgd’

will create a new variant and assigns the value “CJ02-6abbgd” to it.

You can use variants both in numeric and in string calculations. Suppose the variable modemSpeed has been declared as variant with the following statement:

,Dim modemSpeed

and later in your code you assign the following value to it:

modemSpeed = ‘28.8 ‘

The modemSpeed variable is a string variable that you can use in statements such as the following:

MsgBox ‘We suggest a ‘ & modemSpeed & .modem’

This statement displays the following message:

‘We suggest a 28.8 modem’

You can also treat the modemspeed variable as a numeric value with the following statement:

MsgBox ‘A • & modemSpeed & .modem can transfer’ & modemSpeed * 1000 / 8 & ‘bytes ~er second’

This statement displays the following message:

“A 28.8 modem can transfer 3600 bytes per second”

The first instance of the modemSpeed variable in the above statement is treated as a string, because. this is the variant’s type according to the assignment statement (we assigned a string to it). The second instance, however, is treated as a number (a single-precision number). Visual Basic converted it to a numeric value because it’s used in a numeric calculation.

Another example of this behavior of variants can be seen in the following statements:

A=. ’10’
B = ’11’
Debuy.Print A + B
Debug. Print A & B

Both Print statements print the string “1011”. You’re asking Visual Basic to add two strings and this is how Visual Basic interprets your intentions. When applied to strings, the + and & operators are identical. If you change the definition of .the second variable to the following:

B = 11

the first Print statement prints 21 (a numeric value), and the second Print statement prints the string “1011” as before.

Visual Basic knows show to handle variables in a way that “makes sense.” The result may not be what you had in mind, but-it certainly is dictated by common sense: If you really want to concatenate the strings “10” and “11”, you should use the & operator, which would tell Visual Basic exactly what to do. Quite impressive, but for many programmers this is a strange behavior that can lead to subtle errors, and they avoid it. It’s up to you to decide whether to use variants and how far you will go with them. Sure, you can perform tricks with variants, but you shouldn’t overuse them to the point that others can’t read your code.

You can also store dates and times in the Variant type. To assign date or time values to variants, surround the values with the pound sign, as follows:

date1= #03/06/1999#

All operations that you can perform on date variables (discussed in the section “Date Variables”) you can also perform with variants, which hold date and time values.

Converting Variable Types

In same situations you will need to convert variables from one type into another: Visual Basic functions that perform data type conversions.

To convert the variable initialized as:

B = CDbl(A)
Suppose you have declared two integers, as follows:’
Dim A As Integer, B As Integer
A = 23
B = 7
The result of the operation A / B will be a single value. The following statement:

Debug.Print A / B

displays the value 3.285714. To get the same result with the greatest possible .. accuracy, use the CDbll function:

Debug.Print: CDb1(A / B)

which displays the value 3.28571438789368. lt’s the-same value expressed as a Double, and therefore, more accurate.

User-Defined Data Types

In the previous sections, we assumed that applications create variables to store Single values. As a matter of fact, most programs store sets of data of different types. For example, a program for balancing your checkbook must store several .pieces of information for each check: the check’s number, its amount, the date, and so on. All these pieces of information arc necessary to process the cheeks, and ideally, they should be stored together.

A structure for storing multiple values (of the same or different type) is called a record. For example, each check in a checkbook-balancing application is stored in separate record, as shown in Figure 3.1. When you recall a given check, you need all the information stored in the record.

Figure 3.1

Figure 3.1

To define a.record in Visual Basic, use the Type statement, which has the following syntax:

Type varType
variable1 As varType
variable2 As varType
variablen As varType
End Type

After this declaration, you have in essence created a new data type that you can in your application. You can declare variables of this type and manipulate them as-you manipulate all other variables (with a little extra typing). The declaration for the record structure shown in Figure 3.1 is:

Type CheckRecord
CheckNumber As Integer
CheckDatp As Date?
CheckAmount As Single
CheckPaidTu As String*50
End Type

The CheekRecord structure can be used in the same way as regular variables. To define variables of this type, use a statement such as this one:

Dim check1 As CheckRecord, check2 As CheckRecord

To assign value to these variables, you must separately assign a value to each one of its components (they are called fields), which can be accessed by combining the name of the variable and the name of a field separated by a period, as follows:

check1.CheckHumber = 275

You can think of the record as an object and its fields as properties. Here are the assignment statements for a check:

You can also create arrays of records with a statement such as the following (arrays are discussed later in this chapter):

Di. Checks(100) As CheckRecord

Bach element in this array is a CheckRecord record and holds all the fields of a given check. To-access the fields of the third element of the array, use the following notation: .

Records are used frequently to read from and write to random access files. For more information using files, see the File Input/Output tutorial on the CD.

Posted on November 4, 2015 in Visual Basic: The Language

Share the Story

Back to Top
Share This