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

B = A ' Creates error message<br />

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

so the Basic interpreter reports an error. The same applies when attempting to assign a<br />

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

order to establish whether the content of the variable to be assigned matches the type of<br />

the target variable. <strong>OpenOffice</strong>.<strong>org</strong> Basic provides the following test functions for this<br />

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

whether a user has 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<br />

this is assigned to the ValidInput variable. If UserInput does not contain a valid number,<br />

ValidInput is assigned the value 0 and an error message is returned.<br />

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

Basic, a corresponding function for checking Boolean values does not exist. The<br />

functionality can, however, be imitated by using the IsBoolean function:<br />

Function IsBoolean(Value As Variant) As Boolean

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

Saved successfully!

Ooh no, something went wrong!