PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Indizierer in Delphi


Kabelsalat
2006-04-12, 19:53:43
Hallo,

Ich versuche einen Indizierer in Delphi (nicht Delphi for .Net) zu realisieren, habe bisher aber keine Lösung gefunden. Wäre klasse, wenn ihr mir auf die Sprünge helfen könntet...

Das meine ich mit Indizierer:


class Test
{
public object this[int intIndex]
{
get
{
//...
}
set
{
//...
}
}
}


Schonmal Danke für eure Hilfe

Kabelsalat

SamStone
2006-04-12, 21:29:52
Ich versteh deinen obrigen Beispielscode zwar nicht, und weiß auch nicht genau was Indizierer sind, aber ich glaube das was du suchst, heißt unter Delphi property.
Einfach mal in der Delphi Hilfe nach suchen.

Kabelsalat
2006-04-13, 00:38:13
Danke für den Tipp, aber wie man Eigenschaften deklariert, weiß ich. Der Beispielcode stellt zwar eine Eigenschaft dar, allerdings von speziellem Typ: Man greift über einen Index zu, sprich z.B. ObjectTest[10] = myObject; Statt Integern lassen sich auch beliebige andere Typen verwenden, lediglich die Deklaration der Eigenschaft muss angepasst werden.

Ich Gehe davon aus, dass sich dies auch in Delphi mittels "property" realisieren lässt, aber wie? Generell finde ich das implementieren von Eigenschaften unter Delphi erheblich komplizierter als in C#, Java oder VB.Net...

Kabelsalat
2006-04-13, 01:34:29
Ich habe die Lösung: http://www.oreilly.com/catalog/delphi/chapter/ch02.html (nach Array Properties suchen). ... deutlich eleganter in anderen Sprachen :(

PS: Statt Indizierer wäre Indexer evtl. der bessere Begriff gewesen.

SamStone
2006-04-13, 12:08:12
Meinst du vielleicht sowas?
type
ArrayIndizierer = (erstesFeld, zweitesFeld, drittesFeld);

var
unserArray: Array [ArrayIndizierer] of Integer;

Dann kannste das so ansprechen:
unserArray[zweitesFeld] etc...

Oder meinst du doch was anderes?

EDIT: Oder meinste doch vielleicht eher sowas wie einen Dictonary Typ, bei dem ein Schlüssel immer einem Wert zugeordnet wird?

Kabelsalat
2006-04-13, 12:46:22
Ich meine genau das:

Array properties

Properties come in scalar and array flavors. An array property cannot be published, but they have many other uses. The array index can be any type, and you can have multidimensional arrays, too. For array-type properties, you must use read and write methods--you cannot map an array-type property directly to an array-type field.

You can designate one array property as the default property. You can refer to the default property by using an object reference and an array subscript without mentioning the property name, as shown in Example 2-10.

Example 2-10: Using a Default Array Property

type
TExample = class
...
property Items[I: Integer]: Integer read GetItem write SetItem;
property Chars[C: Char]: Char read GetChar write SetChar; default;
end;
var
Example: TExample;
I: Integer;
C: Char;
begin
Example := TExample.Create;
I := Example.Items[4]; // Must mention property name explicitly
C := Example['X']; // Array property is default
C := Example.Chars['X']; // Same as previous line

Indexed properties

You can map many properties to a single read or write method by specifying an index number for each property. The index value is passed to the read and write methods to differentiate one property from another.

You can even mix array indices and an index specifier. The reader and writer methods take the array indices as the first arguments, followed by the index specifier.
Default values

A property can also have stored and default directives. This information has no semantic meaning to the Delphi Pascal language, but Delphi's IDE uses this information when storing form descriptions. The value for the stored directive is a Boolean constant, a field of Boolean type, or a method that takes no arguments and returns a Boolean result. The value for the default directive is a constant value of the same type as the property. Only enumerated, integer, and set-type properties can have a default value. The stored and default directives have meaning only for published properties.

To distinguish a default array from a default value, the default array directive comes after the semicolon that ends the property declaration. The default value directive appears as part of the property declaration. See the default directive in Chapter 5 for details.

/edit: Hier die Beschreibung aus der MSDN-Library: http://msdn2.microsoft.com/en-US/library/2549tw02(VS.80).aspx