Difference between revisions of "Documentation/BASIC Guide/Conversion Functions (Runtime Library)"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 14: Line 14:
 
The easiest way to change a variable from one type to another is to use an assignment.
 
The easiest way to change a variable from one type to another is to use an assignment.
  
Dim A As String
+
<source lang="oobas">
Dim B As Integer
+
Dim A As String
+
Dim B As Integer
B = 101  
+
 
A = B
+
B = 101  
 +
A = B
 +
</source>
  
 
In this example, variable <tt>A</tt> is a string, and variable <tt>B</tt> is an integer. {{OOo}} Basic ensures that variable <tt>B</tt> is converted to a string during assignment to variable <tt>A</tt>. This conversion is much more elaborate than it appears: the integer <tt>B</tt> remains in the working memory in the form of a two-byte long number. <tt>A</tt>, 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 <tt>B</tt> to <tt>A</tt>, <tt>B</tt> has to be converted into <tt>A</tt>'s internal format.  
 
In this example, variable <tt>A</tt> is a string, and variable <tt>B</tt> is an integer. {{OOo}} Basic ensures that variable <tt>B</tt> is converted to a string during assignment to variable <tt>A</tt>. This conversion is much more elaborate than it appears: the integer <tt>B</tt> remains in the working memory in the form of a two-byte long number. <tt>A</tt>, 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 <tt>B</tt> to <tt>A</tt>, <tt>B</tt> has to be converted into <tt>A</tt>'s internal format.  
Line 24: Line 26:
 
Unlike most other programming languages, Basic performs type conversion automatically. However, this may have fatal consequences. Upon closer inspection, the following code sequence
 
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
+
<source lang="oobas">
Dim B As Integer
+
Dim A As String
Dim C As Integer
+
Dim B As Integer
+
Dim C As Integer
B = 1  
+
 
C = 1
+
B = 1  
A = B + C
+
C = 1
 +
A = B + C
 +
</source>
  
 
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.
 
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.
Line 38: Line 42:
 
The same applies when using variant variables:  
 
The same applies when using variant variables:  
  
Dim A  
+
<source lang="oobas">
Dim B  
+
Dim A  
Dim C  
+
Dim B  
+
Dim C  
B = 1  
+
 
C = "1"
+
B = 1  
A = B + C
+
C = "1"
 +
A = B + C
 +
</source>
  
 
Since variant variables may contain both numbers and strings, it is unclear whether variable <tt>A</tt> is assigned the number 2 or the string 11.  
 
Since variant variables may contain both numbers and strings, it is unclear whether variable <tt>A</tt> is assigned the number 2 or the string 11.  
Line 62: Line 68:
 
You can use these conversion functions to define how {{OOo}} Basic should perform these type conversion operations:
 
You can use these conversion functions to define how {{OOo}} Basic should perform these type conversion operations:
  
Dim A As String
+
<source lang="oobas">
Dim B As Integer
+
Dim A As String
Dim C As Integer
+
Dim B As Integer
+
Dim C As Integer
B = 1  
+
 
C = 1
+
B = 1  
A = CStr(B + C)        ' B and C are added together first, then
+
C = 1
                        ' converted (produces the number 2)
+
A = CStr(B + C)        ' B and C are added together first, then
A = CStr(B) + CStr(C)  ' B and C are converted into a string,then
+
                        ' converted (produces the number 2)
                        ' combined (produces string "11")
+
A = CStr(B) + CStr(C)  ' B and C are converted into a string,then
 +
                        ' combined (produces string "11")
 +
</source>
  
 
During the first addition in the example, {{OOo}} Basic first adds the integer variables and then converts the result into a chain of characters. <tt>A</tt> is assigned the string <tt>2</tt>. In the second instance, the integer variables are first converted into two strings and then linked with one another by means of the assignment. <tt>A</tt> is therefore assigned the string <tt>11</tt>.
 
During the first addition in the example, {{OOo}} Basic first adds the integer variables and then converts the result into a chain of characters. <tt>A</tt> is assigned the string <tt>2</tt>. In the second instance, the integer variables are first converted into two strings and then linked with one another by means of the assignment. <tt>A</tt> is therefore assigned the string <tt>11</tt>.
Line 79: Line 87:
 
The <tt>Val</tt> function is different from the <tt>Csng, Cdbl</tt> and <tt>Cstr</tt> methods. It converts a string into a number; however it always expects a period to be used as the decimal point symbol.
 
The <tt>Val</tt> function is different from the <tt>Csng, Cdbl</tt> and <tt>Cstr</tt> 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
+
<source lang="oobas">
Dim B As Double
+
Dim A As String
+
Dim B As Double
A = "2.22"
+
 
B = Val(A)      ' Is converted correctly regardless of the
+
A = "2.22"
                ' country-specific settings
+
B = Val(A)      ' Is converted correctly regardless of the
 +
                ' country-specific settings
 +
</source>
  
 
== Checking the Content of Variables ==
 
== Checking the Content of Variables ==
Line 90: Line 100:
 
In some instances, the date cannot be converted:  
 
In some instances, the date cannot be converted:  
  
Dim A As String
+
<source lang="oobas">
Dim B As Date
+
Dim A As String
+
Dim B As Date
A = "test"
+
 
B = A            ' Creates error message
+
A = "test"
 +
B = A            ' Creates error message
 +
</source>
  
 
In the example shown, the assignment of the <tt>test</tt> 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:
 
In the example shown, the assignment of the <tt>test</tt> 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
+
<source lang="oobas">
Dim B As Boolean
+
Dim A As String
+
Dim B As Boolean
A = "test"
+
 
B = A            ' Creates error message
+
A = "test"
 +
B = A            ' Creates error message
 +
</source>
  
 
Again, the basic interpreter reports an error.  
 
Again, the basic interpreter reports an error.  
Line 114: Line 128:
 
These functions are especially useful when querying user input. For example, you can check whether a user has typed a valid number or date.
 
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
+
<source lang="oobas">
  ValidInput = UserInput
+
If IsNumeric(UserInput) Then
Else
+
  ValidInput = UserInput
  ValidInput = 0
+
Else
  MsgBox "Error message."
+
  ValidInput = 0
End If
+
  MsgBox "Error message."
 +
End If
 +
</source>
  
 
In the previous example, if the <tt>UserInput</tt> variable contains a valid numerical value, then this is assigned to the <tt>ValidInput</tt> variable. If <tt>UserInput</tt> does not contain a valid number, <tt>ValidInput</tt> is assigned the value <tt>0</tt> and an error message is returned.
 
In the previous example, if the <tt>UserInput</tt> variable contains a valid numerical value, then this is assigned to the <tt>ValidInput</tt> variable. If <tt>UserInput</tt> does not contain a valid number, <tt>ValidInput</tt> is assigned the value <tt>0</tt> and an error message is returned.
Line 125: Line 141:
 
While test functions exist for checking numbers, date details and arrays in {{OOo}} Basic, a corresponding function for checking Boolean values does not exist. The functionality can, however, be imitated by using the <tt>IsBoolean</tt> function:  
 
While test functions exist for checking numbers, date details and arrays in {{OOo}} Basic, a corresponding function for checking Boolean values does not exist. The functionality can, however, be imitated by using the <tt>IsBoolean</tt> function:  
  
Function IsBoolean(Value As Variant) As Boolean
+
<source lang="oobas">
  On Error Goto ErrorIsBoolean:
+
Function IsBoolean(Value As Variant) As Boolean
  Dim Dummy As Boolean
+
  On Error Goto ErrorIsBoolean:
  Dummy = Value
+
  Dim Dummy As Boolean
  IsBoolean = True
+
  Dummy = Value
  On Error Goto 0
+
  IsBoolean = True
  Exit Sub
+
  On Error Goto 0
+
  Exit Sub
  ErrorIsBoolean:
+
 
  IsBoolean = False
+
  ErrorIsBoolean:
  On Error Goto 0
+
  IsBoolean = False
End Function
+
  On Error Goto 0
 +
End Function
 +
</source>
  
 
The <tt>IsBoolean</tt> function defines an internal <tt>Dummy</tt> help variable of the Boolean type and tries to assign this to the transferred value. If assignment is successful, the function returns <tt>True</tt>. If it fails, a runtime error is produced, which intercepts the test function to return an error.
 
The <tt>IsBoolean</tt> function defines an internal <tt>Dummy</tt> help variable of the Boolean type and tries to assign this to the transferred value. If assignment is successful, the function returns <tt>True</tt>. If it fails, a runtime error is produced, which intercepts the test function to return an error.

Revision as of 13:22, 2 April 2008


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. Apache OpenOffice 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, Apache OpenOffice Basic offers a range of conversion functions, which you can use to define when the data type of an operation should be converted:

CStr(Var)
converts any data type into a string.
CInt(Var)
converts any data types into an integer value.
CLng(Var)
converts any data types into a long value.
CSng(Var)
converts any data types into a single value.
CDbl(Var)
converts any data types into a double value.
CBool(Var)
converts any data types into a Boolean value.
CDate(Var)
converts any data types into a date value.

You can use these conversion functions to define how Apache OpenOffice 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 (produces the number 2)
A = CStr(B) + CStr(C)   ' B and C are converted into a string,then
                        ' combined (produces string "11")

During the first addition in the example, Apache OpenOffice 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. Apache OpenOffice Basic provides the following test functions for this purpose:

IsNumeric(Value)
checks whether a value is a number.
IsDate(Value)
checks whether a value is a date.
IsArray(Value)
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
Else
  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 Apache OpenOffice 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
 
  ErrorIsBoolean:
  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, which intercepts the test function to return an error.

Template:Documentation/Note

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