![]() |
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:
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.
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:[]) ) ) ); Hat jemand eine idee wie ich das machhen kann? (Ach ja, die Paletten können unterschiedlich viele komponenten enthalten) |
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. |
Re: Konstantes Array aus Records die variables Array enthalt
du meinst so? :
Delphi-Quellcode:
Dann bekomm ich "Undefinierter Bezeichner: 'N'"
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:[]) ) ) ); Selbiges wenn ich CompRec so deklariere:
Delphi-Quellcode:
var CompRec:array [0..1] of tCmeComponentTab;
|
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. |
Re: Konstantes Array aus Records die variables Array enthalt
ja, das sieht dochgut aus, danke
|
Re: Konstantes Array aus Records die variables Array enthalt
Ich hab noch ne Alternative mit etwas mehr Konstanten: :mrgreen:
Delphi-Quellcode:
Nicos Variablennamen sind natürlich besser. :-)
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. HTH, Uli. |
Re: Konstantes Array aus Records die variables Array enthalt
Zitat:
Wenn man beides kombiniert, dann könnte es so aussehen:
Delphi-Quellcode:
Sieht zwar immer noch 'hässlich' aus, aber für übersichtlicheren Code müsste man das Struktur/Schnittstellen-Design ändern...
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. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:50 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz