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

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Bereik)
(With...End With)
Line 47: Line 47:
  
 
== '''<tt>With...End With</tt>''' ==
 
== '''<tt>With...End With</tt>''' ==
=== Qualifiers ===
+
=== Kwalificaties ===
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 het algemeen kijkt BASIC niet in een container, zoals een <tt>Object</tt>, om te zien welke namen daar zouden kunnen zijn gedefinieerd. Als u een dergelijke naam wilt gebruiken, zult u BASIC moeten vertellen waar het moet kijken. U doet dat door de naam van het object te gebruiken als een ''kwalificatie''. Schrijf het vóór de binnenste naam en scheidt ze met een punt:
  
  MyObject.SomeName
+
  MijnObject.EenNaam
  
Since containers may hold other containers, you may need more than one qualifier. Write the qualifiers in order, from outer to inner:
+
Omdat containers ander containers zouden kunnen bevatten, zou u misschien meer dan één kwalificatie nodig hebben. Schrijf de kwalificaties in volgorde, van buiten naar binnen:
  
  OuterObject.InnerObject.FarInsideObject.SomeName
+
  BuitensteObject.BinnesteObject.VerBinnenObject.EenNaam
  
These names may also be described as, "concatenated with the dot-operator ('.')".
+
Deze namen kunnen ook worden beschreven als "samengevoegd met de operator punt ('.')".
  
=== The <tt>With</tt> Alternative ===
+
=== Het alternatief <tt>With</tt> ===
  
 
The <tt>With...End With</tt> 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 <tt>With</tt> statement. Until Basic encounters the <tt>End With</tt> statement, it looks for ''partly-qualified'' names: names that begin with a period (unary dot-operator). The compiler uses the qualifiers from the <tt>With</tt> as though they were written in front of the partly-qualified name.
 
The <tt>With...End With</tt> 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 <tt>With</tt> statement. Until Basic encounters the <tt>End With</tt> statement, it looks for ''partly-qualified'' names: names that begin with a period (unary dot-operator). The compiler uses the qualifiers from the <tt>With</tt> as though they were written in front of the partly-qualified name.

Revision as of 13:03, 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 een 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

Kwalificaties

In het algemeen kijkt BASIC niet in een container, zoals een Object, om te zien welke namen daar zouden kunnen zijn gedefinieerd. Als u een dergelijke naam wilt gebruiken, zult u BASIC moeten vertellen waar het moet kijken. U doet dat door de naam van het object te gebruiken als een kwalificatie. Schrijf het vóór de binnenste naam en scheidt ze met een punt:

MijnObject.EenNaam

Omdat containers ander containers zouden kunnen bevatten, zou u misschien meer dan één kwalificatie nodig hebben. Schrijf de kwalificaties in volgorde, van buiten naar binnen:

BuitensteObject.BinnesteObject.VerBinnenObject.EenNaam

Deze namen kunnen ook worden beschreven als "samengevoegd met de operator punt ('.')".

Het alternatief With

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