Autor Beitrag
Freiberger
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

WIN XP
Delphi 7
BeitragVerfasst: Sa 12.04.08 22:01 
Hallo,

wie kann man eine Klasse erstellen, welche ein Array von Klassen besitzt.
So was wie eine TStringList mit eingenen Daten.

Mein Code bringt nur Fehler...


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
type TmbData = class              
  private
    FHeight: integer;
    FTop: integer;
    FCaption: ShortString;
  protected
  public
    property Caption: ShortString read FCaption write FCaption;
    property Top: integer read FTop write FTop;
    property Height: integer read FHeight write FHeight;
  end;

type TmbDataGroup = class
  private
    Fcount: integer;
    FItems: Array TmbData;
    procedure Setcount(const Value: integer);
  protected
  public
    property Count: integer read FCount write Setcount;
    property Items: Array of TmbData read FItems write FItems;
  end;


Moderiert von user profile iconChristian S.: Color- durch Delphi-Tag ersetzt
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Sa 12.04.08 22:05 
ausblenden Delphi-Quelltext
1:
FItems: Array TmbData;					

Da fehlt ein of ! So, wie es jetzt ist, macht es jedenfalls keinen Sinn :)
Freiberger Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

WIN XP
Delphi 7
BeitragVerfasst: Sa 12.04.08 22:12 
Ja Sorry,
habe ich hier falsch reinkopiert, da ich die Namen ändern mußte.

Ich bekomme aber Fehler...

[Fehler] mbDataGroup.pas(21): Bezeichner erwartet, aber 'ARRAY' gefunden
[Fehler] mbDataGroup.pas(21): Inkompatible Typen: 'dynamic array' und 'TmbData'
...
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Sa 12.04.08 23:07 
Du kannst bei Properties kein Array of als Konstrukt verwenden.
Einzige mir bekannte Möglichkeit wäre dies mit Hilfe eines weiteren selbstdefinierten Array-Typ zu lösen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
type
 TARmbData = Array of TmbData;

type TmbDataGroup = class
  private
    Fcount: integer;
    FItems: TARmbData;
    procedure Setcount(const Value: integer);
  protected
  public
    property Count: integer read FCount write Setcount;
    property Items: TARmbData read FItems write FItems;
  end;

Weiter musst Du die Länge deines Dynamischen Arrays natürlich erst festlegen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var
 mbData: TmbData;
 mbDataGroup: TmbDataGroup;
begin
// Nicht nötig, wenn nur(!) auf FItems zugegriffen wird
// mbData := TmbData.Create;  
// mbDataGroup := TmbDataGroup.Create; 

 Setlength(mbDataGroup.FItems,4);
 mbDataGroup.Items[2] := TmbData.Create;
 mbDataGroup.Items[2].Top := 10;
end;


Cheers,
Marc.
Freiberger Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

WIN XP
Delphi 7
BeitragVerfasst: So 13.04.08 14:28 
Hee,
jetzt klappt es. Da hätte ich ja ewig probieren können.

Dankeschön...
:lol:
Freiberger Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

WIN XP
Delphi 7
BeitragVerfasst: Mo 14.04.08 11:13 
Hallo, ich hätte da noch ne Frage...

Kann ich in einer Klasse ein variables Feld einbinden, was mir dann je nach Zuweisung nur bestimmte Propertys zurück gibt ?

Es solle eine DataGroup geben, die viele Gruppen enthält
jeder Gruppe wird zugewiesen, ob sie Analog-, Digital- oder Textwerte enthalten soll
Dann soll darunter ein Array von Werten stehen

zB: Gruppe[1]= Analogwerte
Gruppe[1].Items[50]:= 12345
oder: Gruppe[1]= Textwerte
Gruppe[1].Items[50]:= 'Hallo'


Hier mein Code.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;


type TDataListAnalog = class
  private
    FValue: integer;        // wird später als Array geändert
  public
    property Value : integer read FValue write FValue;
  end;


type TDataListDigital = class
  private
    FValue: boolean;        // wird später als Array geändert
  public
    property Value : boolean read FValue write FValue;
  end;

type TDataListText = class
  private
    FText: string;          // wird später als Array geändert
  public
    property Text : string read FText write FText;
  end;


type TDataList = Array of TObject;

type DataGroup = Class
  private
    FData : TDataList;
    FCount: integer;
    procedure SetCount(const Value: integer);
  public
    property Count : integer read FCount write SetCount;
    property Data : TDataList read FData write FData;
  end;



implementation

{$R *.dfm}

{ DataGroup }

procedure DataGroup.SetCount(const Value: integer);
begin
  FCount := Value;
  SetLength( FData , value );
end;


procedure TForm1.Button1Click(Sender: TObject);
var DG: DataGroup;
    t: integer;
begin
  DG:= DataGroup.Create;
  DG.SetCount(3);

  DG.Data[0]:= TDataListDigital.create;
  DG.Data[0].value:= True;

  DG.Data[1]:= TDataListText.create;
  DG.Data[1].caption:= 'Test';

  DG.Data[2]:= TDataListAnalog.create;
  DG.Data[2].value:= 12345;


  for t:= 0 to DG.Count-1 do begin
    if DG.Data[t] is TDataText then showmessage( DG.Data[t].caption );
  end;
end;

end.


Moderiert von user profile iconChristian S.: Color- durch Delphi-Tags ersetzt
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mo 14.04.08 11:16 
1. Neue Frage = neus Thema
2. Könntest du hier im Forum bitte Delphi-TAGs verwenden, so wie es deine Vorposter getan haben?
Freiberger Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

WIN XP
Delphi 7
BeitragVerfasst: Mo 14.04.08 11:18 
Sorry, das ist kein neues Thema.
Es geht immernoch um das gleiche Problem wie oben.

Wie verwende ich Delphi-Tags ?


Zuletzt bearbeitet von Freiberger am Mo 14.04.08 11:22, insgesamt 1-mal bearbeitet
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mo 14.04.08 11:22 
Im Antwort-Editor "Bereiche" anklicken, und dort Delphi auswählen. Dann den Quellcode markieren, und auf das + klicken
Freiberger Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

WIN XP
Delphi 7
BeitragVerfasst: Mo 14.04.08 11:34 
OK, Danke. Hatte schonmal in der Hilfe nachgeschaut, aber dort habe ich es nicht gefunden...