Difference between revisions of "Documentation/BASIC Guide/Other Instructions"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Example 1: A User-defined Struct)
m
 
Line 18: Line 18:
 
With the <tt>Type...End Type</tt> statements, you can define your own (non-UNO) structs:
 
With the <tt>Type...End Type</tt> statements, you can define your own (non-UNO) structs:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Type aMenuItem 'assign the name of the type
 
Type aMenuItem 'assign the name of the type
 
     'Define the data fields within the struct. Each
 
     'Define the data fields within the struct. Each
Line 25: Line 25:
 
     aText as String
 
     aText as String
 
End Type 'close the definition
 
End Type 'close the definition
</source>
+
</syntaxhighlight>
  
 
=== Instance ===
 
=== Instance ===
Line 31: Line 31:
 
The <tt>Type</tt> definition is only a pattern or template, not a set of actual variables. To make an ''instance'' of the type — actual variables that can be read and stored — use the <tt>Dim as New</tt> statement:
 
The <tt>Type</tt> definition is only a pattern or template, not a set of actual variables. To make an ''instance'' of the type — actual variables that can be read and stored — use the <tt>Dim as New</tt> statement:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim maItem as New aMenuItem
 
Dim maItem as New aMenuItem
</source>
+
</syntaxhighlight>
  
 
=== Scope ===
 
=== Scope ===
Line 48: Line 48:
 
=== Qualifiers ===
 
=== Qualifiers ===
 
In general, Basic does not look inside a container, such as an <tt>Object</tt>, to see what names might be defined there. If you want to use such a name, you must tell Basic where to look. You do that by using the name of the object as a ''qualifier''. Write it before the inner name, and separate it by a period:
 
In general, Basic does not look inside a container, such as an <tt>Object</tt>, to see what names might be defined there. If you want to use such a name, you must tell Basic where to look. You do that by using the name of the object as a ''qualifier''. Write it before the inner name, and separate it by a period:
 
+
<syntaxhighlight lang="oobas">
 
  MyObject.SomeName
 
  MyObject.SomeName
 
+
</syntaxhighlight>
 
Since containers may hold other containers, you may need more than one qualifier. Write the qualifiers in order, from outer to inner:
 
Since containers may hold other containers, you may need more than one qualifier. Write the qualifiers in order, from outer to inner:
 
+
<syntaxhighlight lang="oobas">
 
  OuterObject.InnerObject.FarInsideObject.SomeName
 
  OuterObject.InnerObject.FarInsideObject.SomeName
 
+
</syntaxhighlight>
 
These names may also be described as, "concatenated with the dot-operator ('.')".
 
These names may also be described as, "concatenated with the dot-operator ('.')".
  
Line 65: Line 65:
 
This example shows how you may define and use a struct, and how to reference the items within it, both with and without <tt>With</tt>. Either way, the names of the data fields (from the <tt>Type</tt> definition) must be qualified by the name of the instance (from the <tt>Dim</tt> statement).
 
This example shows how you may define and use a struct, and how to reference the items within it, both with and without <tt>With</tt>. Either way, the names of the data fields (from the <tt>Type</tt> definition) must be qualified by the name of the instance (from the <tt>Dim</tt> statement).
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Type aMenuItem
 
Type aMenuItem
 
     aCommand as String
 
     aCommand as String
Line 83: Line 83:
 
             & "Text: " & maItem.aText
 
             & "Text: " & maItem.aText
 
End Sub
 
End Sub
</source>
+
</syntaxhighlight>
  
 
=== Example 2: Case statement ===
 
=== Example 2: Case statement ===
Line 89: Line 89:
 
In [[Documentation/BASIC_Guide/Cells and Ranges|Cells and Ranges]], the following example has the qualifiers in the <tt>Case</tt> statements written out completely, for clarity.  You can write it more easily, this way:
 
In [[Documentation/BASIC_Guide/Cells and Ranges|Cells and Ranges]], the following example has the qualifiers in the <tt>Case</tt> statements written out completely, for clarity.  You can write it more easily, this way:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Doc As Object
 
Dim Doc As Object
 
Dim Sheet As Object
 
Dim Sheet As Object
Line 112: Line 112:
 
   End Select
 
   End Select
 
End With
 
End With
</source>
+
</syntaxhighlight>
 
Notice that the <tt>With</tt> construct must be entirely outside of the <tt>Select</tt> construct.
 
Notice that the <tt>With</tt> construct must be entirely outside of the <tt>Select</tt> construct.
  
 
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Other Instructions}}
 
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Other Instructions}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 12:05, 30 January 2021


Type...End Type

Struct

A struct is an ordered collection of data fields, that can be manipulated as a single item. In other terms, you may think of a struct as a record, or part of a record.

The API often uses pre-defined structs, but these are UNO structs, a highly-specialized kind of struct.

Definition

With the Type...End Type statements, you can define your own (non-UNO) structs:

Type aMenuItem			'assign the name of the type
    'Define the data fields within the struct. Each
    ' definition looks like a Dim statement, without the "Dim".
    aCommand as String		
    aText as String
End Type			'close the definition

Instance

The Type definition is only a pattern or template, not a set of actual variables. To make an instance of the type — actual variables that can be read and stored — use the Dim as New statement:

Dim maItem as New aMenuItem

Scope

As shown in the example below, the Type definition may be written at the start of a module (before the first Sub or Function). The definition will then be available to all routines in the module.

As of OpenOffice.org Version 3.0, unlike variables, there is no way to make the definition accessible outside of the module.

An instance of the new type is a variable, and follows the usual rules for variable scope (see Scope and Life Span of Variables).

An example of how to use the definition, and how to reference the fields within an instance, appears in the section on With...End With.

With...End With

Qualifiers

In general, Basic does not look inside a container, such as an Object, to see what names might be defined there. If you want to use such a name, you must tell Basic where to look. You do that by using the name of the object as a qualifier. Write it before the inner name, and separate it by a period:

 MyObject.SomeName

Since containers may hold other containers, you may need more than one qualifier. Write the qualifiers in order, from outer to inner:

 OuterObject.InnerObject.FarInsideObject.SomeName

These names may also be described as, "concatenated with the dot-operator ('.')".

The With Alternative

The With...End With bracketing statements provide an alternative to writing out all the qualifiers, every time – and some of the qualifiers in the API can be quite long. You specify the qualifiers in the With statement. Until Basic encounters the End With statement, it looks for partly-qualified names: names that begin with a period (unary dot-operator). The compiler uses the qualifiers from the With as though they were written in front of the partly-qualified name.

Example 1: A User-defined Struct

This example shows how you may define and use a struct, and how to reference the items within it, both with and without With. Either way, the names of the data fields (from the Type definition) must be qualified by the name of the instance (from the Dim statement).

Type aMenuItem
    aCommand as String
    aText as String
End Type
 
Sub Main
    'Create an instance of the user-defined struct.
    ' Note the keyword, "New".
    Dim maItem as New aMenuItem
    With maItem
        .aCommand = ".uno:Copy"
        .aText = "~Copy"
    End With
 
    MsgBox "Command: " & maItem.aCommand & Chr(13) _
            & "Text: " & maItem.aText
End Sub

Example 2: Case statement

In Cells and Ranges, the following example has the qualifiers in the Case statements written out completely, for clarity. You can write it more easily, this way:

Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object
 
Doc = ThisComponent
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(1,1)	'Cell "B2" (0-based!)
 
Cell.Value = 1000
 
With com.sun.star.table.CellContentType
  Select Case Cell.Type
    Case .EMPTY 
      MsgBox "Content: Empty"
    Case .VALUE
      MsgBox "Content: Value"
    Case .TEXT
      MsgBox "Content: Text"
    Case .FORMULA
      MsgBox "Content: Formula"
  End Select
End With

Notice that the With construct must be entirely outside of the Select construct.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools