Other Instructions

From Wiki
Jump to navigationJump to search


Type...End Type

A struct is a collection of data fields, that can be manipulated as a single item. In older 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. fr:FR/Documentation/BASIC Guide/Other Instructions hu:HU/Documentation/BASIC Guide/Other Instructions it:IT/Documentation/BASIC Guide/Other Instructions ja:JA/Documentation/BASIC Guide/Other Instructions zh:ZH/Documentation/BASIC Guide/Other Instructions


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