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

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Struct)
m (Struct)
Line 11: Line 11:
 
=== Struct ===
 
=== Struct ===
  
Een ''struct'' is een geordende collectie van gegevensvelden, die kunnen wordne bewerkt als één enkel item. Met andere woorden: u kunt aan een struct denken als aan een record, of gedeelte van een record.
+
Een ''struct'' is een geordende collectie van gegevensvelden, die kunnen worden bewerkt als één enkel item. Met andere woorden: u kunt aan een struct denken als aan een record, of gedeelte van een record.
  
 
De [[Documentation/BASIC Guide/API Intro|API]] gebruikt vaak vooraf gedefinieerde structs, maar dat zijn ''UNO structs'', een zeer gespecialiseerd soort struct.
 
De [[Documentation/BASIC Guide/API Intro|API]] gebruikt vaak vooraf gedefinieerde structs, maar dat zijn ''UNO structs'', een zeer gespecialiseerd soort struct.

Revision as of 12:55, 26 January 2013

Book.png

Type...End Type

Struct

Een struct is een geordende collectie van gegevensvelden, die kunnen worden bewerkt als één enkel item. Met andere woorden: u kunt aan een struct denken als aan een record, of gedeelte van een record.

De API gebruikt vaak vooraf gedefinieerde structs, maar dat zijn UNO structs, een zeer gespecialiseerd soort struct.

Definitie

Met de argumenten Type...End Type kunt u uw eigen (niet-UNO) structs definiëren:

Type aMenuItem			'toewijzen van de naam van het type
    'Definieer de gegevensvelden binnen de struct. Elke
    ' definitie ziet er uit als een argument Dim, zonder de "Dim".
    aCommand as String		
    aText as String
End Type			'sluit de definitie

Instantie

De definitie Type is slechts een patroon of sjabloon, geen verzameling van echte variabelen. Gebruik het argument Dim as New om een instance te maken van het type — echte variabelen die kunnen worden gelezen en opgeslagen —:

Dim maItem as New aMenuItem

Bereik

Zoals weergegeven in het voorbeeld hieronder kan de definitie Type aan het begin van ene module worden geschreven (vóór de eerste Sub of Function). De definitie zal dan beschikbaar zijn voor alle routines in de module.

Vanaf OpenOffice.org versie 3.0 is er, anders dan met variabelen, geen manier om de definitie toegankelijke te maken buiten de module.

Een instantie van het nieuwe type is een variabele en volgt de gebruikelijke regels voor het bereik van variabelen (bekijk Bereik en levensduur van variabelen).

Een voorbeeld van het gebruiken van de definitie, en hoe naar de velden in een instantie te verwijzen, staat in het gedeelte over 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