21.08.2013 Views

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Conversion Functions 44<br />

Dim A As String<br />

Dim B As Integer<br />

Dim C As Integer<br />

B = 1<br />

C = 1<br />

A = B + C<br />

which at first glance seems straightforward, ultimately proves to be something of a trap.<br />

The Basic interpreter first calculates the result of the addition process and then converts<br />

this into a string, which, as its result, produces the string 2.<br />

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

string and applies the plus 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<br />

variable A is assigned the number 2 or the string 11.<br />

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

programming; for example, 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<br />

a range of conversion functions, which you can use to define when the data type of an<br />

operation should be converted:<br />

CStr(Var)<br />

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

CInt(Var)<br />

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

CLng(Var)<br />

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

CSng(Var)<br />

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

CDbl(Var)<br />

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

CBool(Var)

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

Saved successfully!

Ooh no, something went wrong!