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

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 10: Line 10:
 
== <tt>Type...End Type</tt> ==
 
== <tt>Type...End Type</tt> ==
  
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.
 
 
Une ''structure'' est une collection de variables qui peuvent être manipulées comme une seule entité. En d'autres termes, on peut faire l'analogie entre une structure et une enregistrement, ou un morceau d'enregistrement.
 
Une ''structure'' est une collection de variables qui peuvent être manipulées comme une seule entité. En d'autres termes, on peut faire l'analogie entre une structure et une enregistrement, ou un morceau d'enregistrement.
  
The [[Documentation/BASIC Guide/API Intro|API]] often uses pre-defined structs, but these are ''UNO structs'', a highly-specialized kind of struct.
 
 
L'[[Documentation/BASIC Guide/API Intro|API]] utilise souvent des structures prédefinies mais ce sont des ''structures UNO'', un type de structure très specialisé
 
L'[[Documentation/BASIC Guide/API Intro|API]] utilise souvent des structures prédefinies mais ce sont des ''structures UNO'', un type de structure très specialisé
  
Line 19: Line 17:
 
=== Définition ===
 
=== Définition ===
  
With the <tt>Type...End Type</tt> statements, you can define your own (non-UNO) structs:
 
 
Avec l'instruction <tt>Type...End Type</tt>, vous pouvez définir votre propre structure (non-UNO)
 
Avec l'instruction <tt>Type...End Type</tt>, vous pouvez définir votre propre structure (non-UNO)
  
Line 33: Line 30:
 
=== Instance ===
 
=== Instance ===
  
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:
 
 
La définition <tt>Type</tt> n'est qu'un modèle, pas l'ensemble de variables existantes. Pour avoir une ''instance'' de ce type, véritables variables qui peuvent etre manipulées, utiliser l'instruction <tt>Dim ... as New</tt> :
 
La définition <tt>Type</tt> n'est qu'un modèle, pas l'ensemble de variables existantes. Pour avoir une ''instance'' de ce type, véritables variables qui peuvent etre manipulées, utiliser l'instruction <tt>Dim ... as New</tt> :
  

Revision as of 07:30, 20 September 2009


Type...End Type

Une structure est une collection de variables qui peuvent être manipulées comme une seule entité. En d'autres termes, on peut faire l'analogie entre une structure et une enregistrement, ou un morceau d'enregistrement.

L'API utilise souvent des structures prédefinies mais ce sont des structures UNO, un type de structure très specialisé


Définition

Avec l'instruction Type...End Type, vous pouvez définir votre propre structure (non-UNO)

Type aMenuItem			'déclare le nom de la structure
    'Défini les champs dans la structure.
    'chaque définition ressemble à une instruction DIM, sabs la commande "Dim"
    aCommand as String		
    aText as String
End Type			'ferme la définition

Instance

La définition Type n'est qu'un modèle, pas l'ensemble de variables existantes. Pour avoir une instance de ce type, véritables variables qui peuvent etre manipulées, utiliser l'instruction Dim ... as New :

Dim maItem as New aMenuItem

Portée

La définition de Type doit être écrite au début du module (avant lea première instruction Sub ou Function)

Pour le version 3.0 de OpenOffice.org, contrairement aux variables, il est impossible de rendre le type déclaré disponible à un autre module.

Une instance du nouveau type est une variable et répond donc aux règles de portées des variables (voir Scope and Life Span of Variables).

Un exemple de définition et d'utilisation d'un champ dans une instance est donné dans la section 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 = StarDesktop.CurrentComponent
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