Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Konstantes Array aus Records die variables Array enthalten ? (https://www.delphipraxis.net/114616-konstantes-array-aus-records-die-variables-array-enthalten.html)

kaiser1401 28. Mai 2008 10:54


Konstantes Array aus Records die variables Array enthalten ?
 
Hallo,

ich habe einige verschachtelte Typen aus Records und dynamischen Arrays. Nun möchte ich ein konstantes array mit records füllen. Soweit so gut. Problem ist nur wenn die Records ein variables array aus records enthalten. Hoffe es war halbwegs verständlich, hier nochmal der relevante Code:

Delphi-Quellcode:
type
    tCmeMaskType = (mtSearch, mtImport);
    tCmeMaskTypes = set of tCmeMaskType;

    tCmeComponent = record
      C:TControl; // Component
      R:tCmeMaskTypes; // RestrictedToMaskTypes
     end;


    tCmeComponentTab = record
      N:string; // TabName
      Co: array of tCmeComponent; // Components
     end;

    tCmeComponentCollection=array of tCmeComponentTab;

implementation

  const CompRec: array [0..1] of tCmeComponentTab =
        (
            (
             // Standard Palette
             N:'Standard';
             Co: (
                  (c:TLabel; R:[] ),
                  (c:TEdit; R:[mtSearch])
                 )
            ),
            (
             // Additional
             N:'Additional';
             Co: (
                  (c:TMemo; R:[])
                 )
            )
        );
Sinn der Sache ist es eine Unit zu haben in der ich diese Verschachtlung einmal konstant aufbaue damit ich später nur hier was ändern muss wenn neue Componenten und Paletten dazu kommen.

Hat jemand eine idee wie ich das machhen kann? (Ach ja, die Paletten können unterschiedlich viele komponenten enthalten)

uligerhardt 28. Mai 2008 11:07

Re: Konstantes Array aus Records die variables Array enthalt
 
AFAIK kannst du dynamische Arrays nicht als Konstanten anlegen.
Du könntest mal versuchen, CompRec als Variable zu deklarieren und im initialization-Teil der Unit - Überraschung :lol: - initialisieren.

kaiser1401 28. Mai 2008 11:12

Re: Konstantes Array aus Records die variables Array enthalt
 
du meinst so? :

Delphi-Quellcode:
implementation

 var CompRec:tCmeComponentCollection;

 initialization
  CompRec:=
        (
            (
             // Standard Palette
             N:'Standard';
             Co: (
                  (c:TLabel; R:[] ),
                  (c:TEdit; R:[mtSearch])
                 )
            ),
            (
             // Additional
             N:'Additional';
             Co: (
                  (c:TMemo; R:[])
                 )
            )
        );
Dann bekomm ich "Undefinierter Bezeichner: 'N'"

Selbiges wenn ich CompRec so deklariere:
Delphi-Quellcode:
 var CompRec:array [0..1] of tCmeComponentTab;

nicodex 28. Mai 2008 12:27

Re: Konstantes Array aus Records die variables Array enthalt
 
Delphi-Quellcode:
unit Foobar;

interface

uses
  Controls;

type
  TCmeMaskType = (mtSearch, mtImport);
  TCmeMaskTypes = set of TCmeMaskType;
  TCmeComponent = record
    CtrlClass: TControlClass;
    MaskTypes: TCmeMaskTypes;
  end;
  TCmeComponentTab = record
    TabName: string;
    Components: array of TCmeComponent;
  end;
  TCmeComponentCollection = array of TCmeComponentTab;

implementation

uses
  StdCtrls;

var
  GCompRec: array [0..1] of TCmeComponentTab = (
    (
      TabName: 'Standard';
      Components: nil // set in InitCompRec()
    ),
    (
      TabName: 'Additional';
      Components: nil // set in InitCompRec()
    )
  );

procedure InitCompRec();
begin
  with GCompRec[0] do
  begin
    SetLength(Components, 2);
    with Components[0] do
    begin
      CtrlClass := TLabel;
      MaskTypes := [];
    end;
    with Components[1] do
    begin
      CtrlClass := TEdit;
      MaskTypes := [mtSearch];
    end;
  end;
  with GCompRec[1] do
  begin
    SetLength(Components, 1);
    with Components[0] do
    begin
      CtrlClass := TMemo;
      MaskTypes := [];
    end;
  end;
end;

initialization

  InitCompRec();

end.

kaiser1401 28. Mai 2008 12:32

Re: Konstantes Array aus Records die variables Array enthalt
 
ja, das sieht dochgut aus, danke

uligerhardt 28. Mai 2008 12:40

Re: Konstantes Array aus Records die variables Array enthalt
 
Ich hab noch ne Alternative mit etwas mehr Konstanten: :mrgreen:
Delphi-Quellcode:
type
  tCmeMaskType = (mtSearch, mtImport);
  tCmeMaskTypes = set of tCmeMaskType;

  tCmeComponent = record
    C: TControlClass; // Component
    R: tCmeMaskTypes; // RestrictedToMaskTypes
  end;


  tCmeComponentTab = record
    N: string; // TabName
    Co: array of tCmeComponent; // Components
  end;

  tCmeComponentCollection = array of tCmeComponentTab;

implementation

{$R *.dfm}

var
  CompRec: array[0..1] of tCmeComponentTab = (
    (
    // Standard Palette
    N: 'Standard';
    Co: nil; ),
    (
    // Additional
    N: 'Additional';
    Co: nil; )
    );

procedure CmeComponentTab_SetComps(var ATab: tCmeComponentTab; const AComps: array of tCmeComponent);
var
  i: Integer;
begin
  SetLength(ATab.Co, Length(AComps));
  for i := Low(AComps) to High(AComps) do
    ATab.Co[i] := AComps[i];
end;

const
  cStandardComps: array[0..1] of tCmeComponent = (
    (c: TLabel; R: []),
    (c: TEdit; R: [mtSearch])
    );

  cAdditionalComps: array[0..0] of tCmeComponent = (
    (c: TMemo; R: [])
    );

initialization
  CmeComponentTab_SetComps(CompRec[0], cStandardComps);
  CmeComponentTab_SetComps(CompRec[1], cAdditionalComps);
end.
Nicos Variablennamen sind natürlich besser. :-)

HTH,
Uli.

nicodex 28. Mai 2008 13:46

Re: Konstantes Array aus Records die variables Array enthalt
 
Zitat:

Zitat von uligerhardt
Nicos Variablennamen sind natürlich besser.

Dafür war ich zu faul die Komponentenlisten 'lesbar' zu initialisieren :)

Wenn man beides kombiniert, dann könnte es so aussehen:
Delphi-Quellcode:
unit Foobar;

interface

uses
  Controls;

type
  TCmeMaskType = (mtSearch, mtImport);
  TCmeMaskTypes = set of TCmeMaskType;
  TCmeComponent = record
    CtrlClass: TControlClass;
    MaskTypes: TCmeMaskTypes;
  end;
  TCmeComponentTab = record
    TabName: string;
    Components: array of TCmeComponent;
  end;
  TCmeComponentCollection = array of TCmeComponentTab;

implementation

uses
  StdCtrls;

var
  GCompRec: TCmeComponentCollection;

procedure InitCompRec();
type
  TCompRec = (
    crStandard,
    crAdditional
  );
  TCompRecStandard = (
    crStandardLabel,
    crStandardEdit
  );
  TCompRecAdditional = (
    crAdditionalMemo
  );
  PCmeComponentArray = ^TCmeComponentArray;
  TCmeComponentArray =
    array [0..High(Integer) div SizeOf(TCmeComponent) - 1] of TCmeComponent;
resourcestring
  StrCompRecTabNameStandard  = 'Standard';
  StrCompRecTabNameAdditional = 'Additional';
const
  CompRecTabNames: array [TCompRec] of string = (
    StrCompRecTabNameStandard,
    StrCompRecTabNameAdditional
  );
  CompRecStandard: array [TCompRecStandard] of TCmeComponent = (
    (
      CtrlClass: TLabel;
      MaskTypes: []
    ),
    (
      CtrlClass: TEdit;
      MaskTypes: [mtSearch]
    )
  );
  CompRecAdditional: array [TCompRecAdditional] of TCmeComponent = (
    (
      CtrlClass: TMemo;
      MaskTypes: []
    )
  );
  CompRecComponents: array [TCompRec] of record
    High: Integer;
    Comp: ^TCmeComponent;
  end = (
    (
      High: Integer(High(CompRecStandard));
      Comp: @CompRecStandard;
    ),
    (
      High: Integer(High(CompRecAdditional));
      Comp: @CompRecAdditional;
    )
  );
var
  Entry: TCompRec;
  Index: Integer;
begin
  SetLength(GCompRec, Integer(High(TCompRec)) + 1);
  for Entry := Low(TCompRec) to High(TCompRec) do
    with GCompRec[Integer(Entry)] do
    begin
      TabName := CompRecTabNames[Entry];
      with CompRecComponents[Entry] do
      begin
        SetLength(Components, High + 1);
        for Index := 0 to High do
          Components[Index] := PCmeComponentArray(Comp)[Index];
      end;
    end;
end;

initialization

  InitCompRec();

end.
Sieht zwar immer noch 'hässlich' aus, aber für übersichtlicheren Code müsste man das Struktur/Schnittstellen-Design ändern...


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:25 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz