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 />

The Val function is different from the Csng, Cdbl and Cstr methods. It converts a string into a number;<br />

however it always expects a period to be used as the decimal point symbol.<br />

Dim A As String<br />

Dim B As Double<br />

A = "2.22"<br />

B = Val(A) ' Is converted correctly regardless of the<br />

' country-specific settings<br />

Checking the Content of Variables<br />

In some instances, the date cannot be converted:<br />

Dim A As String<br />

Dim B As Date<br />

A = "test"<br />

B = A ' Creates error message<br />

In the example shown, the assignment of the test string to a date variable makes no sense, so the Basic<br />

interpreter reports an error. The same applies when attempting to assign a string to a Boolean variable:<br />

Dim A As String<br />

Dim B As Boolean<br />

A = "test"<br />

B = A ' Creates error message<br />

Again, the basic interpreter reports an error.<br />

These error messages can be avoided by checking the program before an assignment, in order to establish<br />

whether the content of the variable to be assigned matches the type of the target variable. <strong>OpenOffice</strong>.<strong>org</strong> Basic<br />

provides the following test functions for this purpose:<br />

IsNumeric(Value)<br />

checks whether a value is a number.<br />

IsDate(Value)<br />

checks whether a value is a date.<br />

IsArray(Value)<br />

checks whether a value is an array.<br />

These functions are especially useful when querying user input. For example, you can check whether a user has<br />

typed a valid number or date.<br />

If IsNumeric(UserInput) Then<br />

ValidInput = UserInput<br />

Else<br />

ValidInput = 0<br />

MsgBox "Error message."<br />

End If<br />

In the previous example, if the UserInput variable contains a valid numerical value, then this is assigned to the<br />

ValidInput variable. If UserInput does not contain a valid number, ValidInput is assigned the value 0 and<br />

an error message is returned.<br />

While test functions exist for checking numbers, date details and arrays in <strong>OpenOffice</strong>.<strong>org</strong> Basic, a corresponding<br />

function for checking Boolean values does not exist. The functionality can, however, be imitated by using the<br />

IsBoolean function:<br />

Function IsBoolean(Value As Variant) As Boolean<br />

On Error Goto ErrorIsBoolean:<br />

Dim Dummy As Boolean<br />

Dummy = Value<br />

IsBoolean = True<br />

On Error Goto 0<br />

Exit Sub<br />

ErrorIsBoolean:<br />

Chapter 3 · Runtime Library 37

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

Saved successfully!

Ooh no, something went wrong!