22.01.2015 Views

OpenOffice.org Macros Explained - LibreOffice-NA.US

OpenOffice.org Macros Explained - LibreOffice-NA.US

OpenOffice.org Macros Explained - LibreOffice-NA.US

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Here are some specific benefits of using constants:<br />

• Constants improve the readability of a macro. The word Gravity is easier to recognize than the value<br />

9.81.<br />

• Constants are easy to manage. If I require greater precision or if the gravitational pull changes, I<br />

have to change the value in only one location.<br />

• Constants help prevent difficult-to-find errors by changing run-time errors into compile-time errors.<br />

Typing “Grevity” rather than “Gravity” is a compile-time error, while mistyping “9.81” as “9.18” is<br />

not.<br />

• While a value like 9.81 may be obvious to you, it may not be as obvious to others reading your code<br />

later. It becomes what programmers call a “magic number,” and experienced programmers try to<br />

avoid magic numbers at all costs. Their unexplained meanings make for difficulties in maintaining<br />

code later, when the original programmer is not available to explain — or has f<strong>org</strong>otten the details<br />

entirely.<br />

TIP<br />

<strong>OpenOffice</strong>.<strong>org</strong> defines the constant Pi. This is a mathematical constant with a value of approximately<br />

3.1415926535897932385.<br />

3.3. The With statement<br />

The With statement is used to simplify accessing complex data types. Listing 16 defines a data type that<br />

contains two different variables: FirstName and LastName. You can access these variables by placing a<br />

period between the variable name and the data item.<br />

Sub ExampleCreateNewType<br />

Dim Person As PersonType<br />

Person.FirstName = "Andrew"<br />

Person.LastName = "Pitonyak"<br />

...<br />

End Sub<br />

Or... the With statement provides a shortcut for accessing multiple data elements from the same variable.<br />

Sub ExampleCreateNewType<br />

Dim Person As PersonType<br />

With Person<br />

.FirstName = "Andrew"<br />

.LastName = "Pitonyak"<br />

End With<br />

...<br />

End Sub<br />

Similarly:<br />

Dim oProp As New com.sun.star.beans.PropertyValue<br />

oProp.Name = "Person" 'Set Name Property<br />

oProp.Value = "Boy Bill" 'Set Value Property<br />

Using With:<br />

Dim oProp As New com.sun.star.beans.PropertyValue<br />

With oProp<br />

.Name = "Person" 'Set Name Property<br />

44

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

Saved successfully!

Ooh no, something went wrong!