21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Conversion Functions<br />

calculates the result of the addition process and then converts this into a string, which, as its result, produces the<br />

string 2.<br />

If, on the other hand, the Basic interpreter first converts the start values B and C into a string and applies the plus<br />

operator to the result, it produces the string 11.<br />

The same applies when using variant variables:<br />

Dim A<br />

Dim B<br />

Dim C<br />

B = 1<br />

C = "1"<br />

A = B + C<br />

Since variant variables may contain both numbers and strings, it is unclear whether variable A is assigned the<br />

number 2 or the string 11.<br />

The error sources noted for implicit type conversions can only be avoided by careful programming; for example,<br />

by not using the variant data type.<br />

To avoid other errors resulting from implicit type conversions, <strong>OpenOffice</strong>.<strong>org</strong> Basic offers a range of conversion<br />

functions, which you can use to define when the data type of an operation should be converted:<br />

CStr(Var)<br />

CInt(Var)<br />

CLng(Var)<br />

CSng(Var)<br />

CDbl(Var)<br />

converts any data type into a string.<br />

converts any data types into an integer value.<br />

converts any data types into a long value.<br />

converts any data types into a single value.<br />

converts any data types into a double value.<br />

CBool(Var)<br />

converts any data types into a Boolean value.<br />

CDate(Var)<br />

converts any data types into a date value.<br />

You can use these conversion functions to define how <strong>OpenOffice</strong>.<strong>org</strong> Basic should perform these type<br />

conversion operations:<br />

Dim A As String<br />

Dim B As Integer<br />

Dim C As Integer<br />

B = 1<br />

C = 1<br />

A = CStr(B + C) ' B and C are added together first, then<br />

' converted to the string "2"<br />

A = CStr(B) + CStr(C) ' B and C are converted into a string,then<br />

' combined to produce the string "11"<br />

During the first addition in the example, <strong>OpenOffice</strong>.<strong>org</strong> Basic first adds the integer variables and then converts<br />

the result into a chain of characters. A is assigned the string 2. In the second instance, the integer variables are first<br />

converted into two strings and then linked with one another by means of the assignment. A is therefore assigned<br />

the string 11.<br />

The numerical CSng and CDbl conversion functions also accept decimal numbers. The symbol defined in the<br />

corresponding country-specific settings must be used as the decimal point symbol. Conversely, the CStr methods<br />

use the currently selected country-specific settings when formatting numbers, dates and time details.<br />

36 <strong>OpenOffice</strong>.<strong>org</strong> 3.2 <strong>BASIC</strong> <strong>Guide</strong> · March 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!