Conversion Functions ( Runtime Library)

From Apache OpenOffice Wiki
Jump to: navigation, search

In many situations, circumstances arise in which a variable of one type has to be changed into a variable of another type.

Implicit and Explicit Type Conversions

The easiest way to change a variable from one type to another is to use an assignment.

Dim A As String
Dim B As Integer
B = 101 
A = B

In this example, variable A is a string, and variable B is an integer. Basic ensures that variable B is converted to a string during assignment to variable A. This conversion is much more elaborate than it appears: the integer B remains in the working memory in the form of a two-byte long number. A, on the other hand, is a string, and the computer saves a one- or two-byte long value for each character (each number). Therefore, before copying the content from B to A, B has to be converted into A's internal format.

Unlike most other programming languages, Basic performs type conversion automatically. However, this may have fatal consequences. Upon closer inspection, the following code sequence

Dim A As String
Dim B As Integer
Dim C As Integer
B = 1 
C = 1
A = B + C

which at first glance seems straightforward, ultimately proves to be something of a trap. The Basic interpreter first calculates the result of the addition process and then converts this into a string, which, as its result, produces the string 2.

If, on the other hand, the Basic interpreter first converts the start values B and C into a string and applies the plus operator to the result, it produces the string 11.

The same applies when using variant variables:

Dim A 
Dim B 
Dim C 
B = 1 
C = "1"
A = B + C

Since variant variables may contain both numbers and strings, it is unclear whether variable A is assigned the number 2 or the string 11.

The error sources noted for implicit type conversions can only be avoided by careful programming; for example, by not using the variant data type.

To avoid other errors resulting from implicit type conversions, Basic offers a range of conversion functions, which you can use to define when the data type of an operation should be converted:

converts any data type into a string.
converts any data types into an integer value.
converts any data types into a long value.
converts any data types into a single value.
converts any data types into a double value.
converts any data types into a Boolean value.
converts any data types into a date value.

You can use these conversion functions to define how Basic should perform these type conversion operations:

Dim A As String
Dim B As Integer
Dim C As Integer
B = 1 
C = 1
A = CStr(B + C)         ' B and C are added together first, then
                        ' converted to the string "2"
A = CStr(B) + CStr(C)   ' B and C are converted into a string,then
                        ' combined to produce the string "11"

During the first addition in the example, Basic first adds the integer variables and then converts the result into a chain of characters. A is assigned the string 2. In the second instance, the integer variables are first converted into two strings and then linked with one another by means of the assignment. A is therefore assigned the string 11.

The numerical CSng and CDbl conversion functions also accept decimal numbers. The symbol defined in the corresponding country-specific settings must be used as the decimal point symbol. Conversely, the CStr methods use the currently selected country-specific settings when formatting numbers, dates and time details.

The Val function is different from the Csng, Cdbl and Cstr methods. It converts a string into a number; however it always expects a period to be used as the decimal point symbol.

Dim A As String
Dim B As Double
A = "2.22"
B = Val(A)      ' Is converted correctly regardless of the
                ' country-specific settings

Checking the Content of Variables

In some instances, the date cannot be converted:

Dim A As String
Dim B As Date
A = "test"
B = A            ' Creates error message

In the example shown, the assignment of the test string to a date variable makes no sense, so the Basic interpreter reports an error. The same applies when attempting to assign a string to a Boolean variable:

Dim A As String
Dim B As Boolean
A = "test"
B = A            ' Creates error message

Again, the basic interpreter reports an error.

These error messages can be avoided by checking the program before an assignment, in order to establish whether the content of the variable to be assigned matches the type of the target variable. Basic provides the following test functions for this purpose:

checks whether a value is a number.
checks whether a value is a date.
checks whether a value is an array.

These functions are especially useful when querying user input. For example, you can check whether a user has typed a valid number or date.

If IsNumeric(UserInput) Then
  ValidInput = UserInput
  ValidInput = 0
  MsgBox "Error message."
End If

In the previous example, if the UserInput variable contains a valid numerical value, then this is assigned to the ValidInput variable. If UserInput does not contain a valid number, ValidInput is assigned the value 0 and an error message is returned.

While test functions exist for checking numbers, date details and arrays in Basic, a corresponding function for checking Boolean values does not exist. The functionality can, however, be imitated by using the IsBoolean function:

Function IsBoolean(Value As Variant) As Boolean
  On Error Goto ErrorIsBoolean:
  Dim Dummy As Boolean
  Dummy = Value
  IsBoolean = True
  On Error Goto 0
  Exit Sub
  IsBoolean = False
  On Error Goto 0
End Function

The IsBoolean function defines an internal Dummy help variable of the Boolean type and tries to assign this to the transferred value. If assignment is successful, the function returns True. If it fails, a runtime error is produced, the error handler intercepts the error, and the function returns False.

Documentation note.png VBA : If a string in Basic contains a non-numerical value and if this is assigned to a number, Basic does not produce an error message, but stops converting the string at the first invalid character. This procedure differs from VBA. There, an error is triggered and program implementation terminated if a corresponding assignment is executed.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools