Autor Beitrag
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mo 28.04.03 00:04 
Hallo Leute,

hab ein Problem: Ich weiß nicht wie ich eine Konstante definieren muss. Die Konstante soll einige Informationen über mehrere Tabellendefinitionen enthalten. Eine Tabellendefinition enthält mehrere Informationen über eine bestimmte Tabelle einer Datenbank. Damit ich diese Informationen verfünftig festlegen kann habe ich ein paar Typen definiert:

Als erstes ein Record welches Informationen über eine Tabelle enthält:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
Type
  tTableDef = Record 
      Name: String;
      PrimaryKey: tTableColumnName;
      Columns: tTableColumns;
    End;

Da jede Tabelle Spalten enthält und jede Spalte auch Eigenschaften besitzt ist der Typ tTableColumns wie folgt definiert:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Type
  tTableColumn = Record
      Name: tTableColumnName;
      Typ: tTableColumnTypeId;
      Length: Integer;
      Key,
      Nullable: Boolean;
    End;

  tTableColumns = Array Of tTableColumn;

Nun habe ich am anfang geschrieben das ich mehrere Tabellen beschreiben muss. Deshalb habe ich für jede Tabelle ein Element in einem Aufzählungstyp eingefügt:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
Type
  tTables = (
      Table1,
      Table2,
      Table3
    );

So und jetzt kommt das Problem. Ich möchte nun eine Konstante definieren welche Informationen über alle Tabellen enthält. Ich habe es wie folgt probiert:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
Const
  cTableDefintions : Array [tTables] of tTableDef = (
      (Name: 'Tabelle 1'; PrimaryKey: ''; Columns: ???), // Table1
      (Name: 'Tabelle 2'; PrimaryKey: ''; Columns: ???), // Table2
      (Name: 'Tabelle 3'; PrimaryKey: ''; Columns: ???)  // Table3
    );

Das Problem ist nun wie ich die Spaltendefinition mit angeben kann. Ich habe es schon so versucht:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Const
  cTable1Columns: Array [1..2] Of tTableColumn = (
      (Name: 'Feld 1'),
      (Name: 'Feld 2')
    );

  cTableDefintions : Array [tTables] of tTableDef = (
      (Name: 'Tabelle 1'; PrimaryKey: ''; Columns: cTable1Columns), // Table1
      (Name: 'Tabelle 2'; PrimaryKey: '' ), // Table2
      (Name: 'Tabelle 3'; PrimaryKey: '')  // Table3
    );

Allerdings bekomme ich dann einen Fehler:
Compiler hat folgendes geschrieben:
[Fehler] zzz.pas(54): Inkompatible Typen: 'Array' und 'tTableColumns'


Ich hoffe Ihr habt mein Problem verstanden und könnt mir eventl. einen Hinweis geben.

Vielen Dank

Gruß
Tino
maximus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Mo 28.04.03 11:00 
Naja...die fehler meldung beschreibt die lösung schon ganz gut :) da cTable1Columns ein array ist, denke ich es geht vielleicht mit cTable1Columns[0].
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Const 
  cTable1Columns: Array [1..2] Of tTableColumn = ( 
      (Name: 'Feld 1'), 
      (Name: 'Feld 2') 
    ); 

  cTableDefintions : Array [tTables] of tTableDef = ( 
      (Name: 'Tabelle 1'; PrimaryKey: ''; Columns: cTable1Columns[0]), // Table1 
      (Name: 'Tabelle 2'; PrimaryKey: '' ), // Table2 
      (Name: 'Tabelle 3'; PrimaryKey: '')  // Table3 
    );
Habs nicht getestet, erscheint mir aber logisch...vermutlich dir auch :D

_________________
mfg.
mâximôv
Tino Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mo 28.04.03 11:13 
Hallo,

maximus hat folgendes geschrieben:
da cTable1Columns ein array ist, denke ich es geht vielleicht mit cTable1Columns[0].

Aber die Variable Columns des Records tTableDef ist vom Type tTableColumns. Mit cTable1Columns[0] würde ich ja nur eine Spaltendefinition an. Ich möchte aber alle Spaltendefinitionen angeben.

Trotzdem Danke für deine Antwort.

Gruß
Tino
maximus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Mo 28.04.03 12:31 
aso...sorry! Hatte mich auch schon gewundert...wär ja zu einfach gewesen :)

cTableColumns müsste eigentlich vom typ tTableColumns sein, gelle. Was aber nicht geht, weil es offen ist und die const nicht! ...hmm. Dann musst du wohl mit pointern arbeiten, die sind ja im allgemeinen unkritischer, weiss aber nicht, ob man den @-operator in constanten verwenden kann!?

PS: Vielleicht mit nem typeCast, musst ma schaun ob dyn.arrays und feste.arrays kompatible sind.

_________________
mfg.
mâximôv
Tino Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mo 28.04.03 12:54 
Hi,

ja, mit dem Pointer habe ich auch schon probiert:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Const 
  cTable1Columns: Array [1..2] Of tTableColumn = ( 
      (Name: 'Feld 1'), 
      (Name: 'Feld 2') 
    ); 
  cTableDefintions : Array [tTables] of tTableDef = ( 
      (Name: 'Tabelle 1'; PrimaryKey: ''; Columns: @cTable1Columns), // Table1 
      (Name: 'Tabelle 2'; PrimaryKey: '' ), // Table2 
      (Name: 'Tabelle 3'; PrimaryKey: '')  // Table3 
    );

Da gab es dann auch keinen Fehler. Nur als ich dann auf die Variable Columns zugreifen wollte gab es eine Access Violation. Auch ein Typ-Cast hat da nicht geholfen. :cry:

Gruß
Tino
maximus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Mo 28.04.03 14:44 
Dann sind dyn.arrays wohl nicht kompatibel! Wie wärs wenn du...
ausblenden Quelltext
1:
2:
pTableColumns = ^tTableColumns;
tTableColumns = Array[0..65535] Of tTableColumn;
...definierst und dir irgendwo zusätzlich die anzahl der tatsächlichen eintrage speicherst?

Dabei seh ich grade, du müsstest oben Columns natürlich als pointer deklarieren:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
Type 
  tTableDef = Record 
      Name: String; 
      PrimaryKey: tTableColumnName; 
      Columns: pTableColumns; //<- pointer
    End;
oder hattest du das schon?

_________________
mfg.
mâximôv