[Delphi] Ideksowany property. Jak zrobic???

0

Pytanie jest takie:

Chce stworzyc property dla arraya i nie wiem jak to zrobic? [glowa]
moja klasa wyglada +- tak

TSwiatla = class (TCustompanel)
private
fCzasySwiatel: array [0..3] of word;
protected
procedure TSwiatla.SetLightsTime (Index: Integer; Value: Word);
function TSwiatla.GetLightsTime(Index: Integer): Word;
public
published
property Green: Word index 0 read GetLightsTime write SetLightsTime;
property Orange: Word index 1 read GetLightsTime write SetLightsTime;
property Red: Word index 2 read GetLightsTime write SetLightsTime;
property OrangeRed: Word index 3 read GetLightsTime write SetLightsTime;
property CzasySwiatel [Index: Integer]: Word read* GetLightsTime write SetLightsTime;
end;

Blad wywala mi tam gdzie jest gwiazdka [???] [???] [???]

Jak ktos mi powie gdzie jest blad a najlepiej go poprawi bede bardzo wdzieczny. [hurra]

Procedura i funkcja na razie jedynie zapisuje wartosc i odczytuje wiec nie ma sensu pisac ich. ;-)

0

Moim zdaniem nie mozesz miec tej samej metody dostępowej do właściwości zwykłych i tablicowych.

0

po pierwsze to jest to prawie ze zerzniete (przepraszam za wyrazenie) z pomocy w delphi ktora przytaczam ponizej :-[

Index specifiers allow several properties to share the same access method while representing different values. An index specifier consists of the directive index followed by an integer constant between –2147483647 and 2147483647. If a property has an index specifier, its read and write specifiers must list methods rather than fields. For example,

type

TRectangle = class
private
FCoordinates: array[0..3] of Longint;
function GetCoordinate(Index: Integer): Longint;
procedure SetCoordinate(Index: Integer; Value: Longint);
public
property Left: Longint index 0 read GetCoordinate write SetCoordinate;
property Top: Longint index 1 read GetCoordinate write SetCoordinate;
property Right: Longint index 2 read GetCoordinate write SetCoordinate;
property Bottom: Longint index 3 read GetCoordinate write SetCoordinate;
property Coordinates[Index: Integer]: Longint read GetCoordinate write SetCoordinate;
...
end;

An access method for a property with an index specifier must take an extra value parameter of type Integer. For a read function, it must be the last parameter; for a write procedure, it must be the second-to-last parameter (preceding the parameter that specifies the property value). When a program accesses the property, the property’s integer constant is automatically passed to the access method.
Given the declaration above, if Rectangle is of type TRectangle, then

Rectangle.Right := Rectangle.Left + 100;

corresponds to

Rectangle.SetCoordinate(2, Rectangle.GetCoordinate(0) + 100);

Sądząc po tym jest to mozliwe a ponadto sadzę zże jest nawet możliwym aby używac tablic wielowymiarowych. 8-0

Jeżeli ktoś wie jaki błąd robię to niech pisze.

0

procedure TSwiatla.SetLightsTime (Index: Integer; Value: Word);
function TSwiatla.GetLightsTime(Index: Integer): Word;

A to kurde co? Wywal to, co zboldowałem, a powinno być ok.

0

procedure TSwiatla.SetLightsTime (Index: Integer; Value: Word);
function TSwiatla.GetLightsTime(Index: Integer): Word;

A to kurde co? Wywal to, co zboldowałem, a powinno być ok.

Nic nie powinno być ok. W części deklaracji (czyli także w protected) nie można stosować TSwiatla. To już jest domyślnie.

A co do problemu to powinno być tak:

TSwiatla = class (TCustompanel)
private
fCzasySwiatel: array [0..3] of word;
protected
procedure SetLightsTime (Index: Integer; Value: Word);
function GetLightsTime(Index: Integer): Word;
public
property CzasySwiatel[Index: Integer]: Word read GetLightsTime write SetLightsTime;
published
property Green: Word index 0 read GetLightsTime write SetLightsTime;
property Orange: Word index 1 read GetLightsTime write SetLightsTime;
property Red: Word index 2 read GetLightsTime write SetLightsTime;
property OrangeRed: Word index 3 read GetLightsTime write SetLightsTime;
end;

Czytaj trochę komunikaty. Tablica nie może być właściwością publikowaną. Jak w IO by to musiało być przedstawione?

0

To akurat to przez pomyłke wylądowało w poscie. Mam na mysli procedure TSwiatla. ... [prosze]

I nie jest ok bo blad wywala. ;-(

Aha jak nie czytałes uwaznie to wczytaj sie w 3 post a dokladnie j przyklad w cytacie z pomocy delphi!!!! :-[

[pomocy] [pomocy] [pomocy] [pomocy]

0

To akurat to przez pomyłke wylądowało w poscie. Mam na mysli procedure TSwiatla. ... [prosze]

I nie jest ok bo blad wywala. ;-(

Aha jak nie czytałes uwaznie to wczytaj sie w 3 post a dokladnie j przyklad w cytacie z pomocy delphi!!!! :-[

[pomocy] [pomocy] [pomocy] [pomocy]

No #@%@$ :-[ Jak to nie działa?
Przecież napisałem, że nie może być w sekcji published (jak wół widać, że w przykłądzie z helpa jest w sekcji public). Jeszcze raz przytaczam poprawny kod razem z miejscem gdzie masz to umieścić:

TSwiatla = class (TCustompanel)
private
fCzasySwiatel: array [0..3] of word;
protected
procedure SetLightsTime (index: Integer; Value: Word);
function GetLightsTime(index: Integer): Word;
public
property CzasySwiatel[index: Integer]: Word read GetLightsTime write SetLightsTime;
published
property Green: Word index 0 read GetLightsTime write SetLightsTime;
property Orange: Word index 1 read GetLightsTime write SetLightsTime;
property Red: Word index 2 read GetLightsTime write SetLightsTime;
property OrangeRed: Word index 3 read GetLightsTime write SetLightsTime;
end;

implementation

procedure TSwiatla.SetLightsTime (index: Integer; Value: Word);
begin
//tutaj obsługa tej procki
end;

function TSwiatla.GetLightsTime(index: Integer): Word;
begin
//a tu tej
end;

I jeżeli dalej wywala komunikat to napisz jaki (to malutkie okienko na dole o ile go nie wyłączyłeś)

1 użytkowników online, w tym zalogowanych: 0, gości: 1