![]() |
Re: wie abspeichern?
:gruebel: Ähh Ja, sicher doch :wiejetzt:
ich versteh jetzt nicht ganz was ihr meint, bzw worüber iohr euch grade im detail streitet, mir scheint aber das die kritikpunkte für mich total sekundär sind ich will einfach nur 1 array haben ich will das so in etwa haben (jetzt ausm kopf):
Code:
also das wär meine lösung (ich bin ein record-fanatiker *g*)
TZustand = record
atem, herzschlag:integer end; TPatient = record name, lauftext:string; story, tipps:TStringlist; zustand:TZustand; end; ... var Patienten:array of TPatient; geht das mit dem package schnell und einfach? |
Re: wie abspeichern?
Arrays sind binärer blödsinn und zum persistenzieren von datensätzen, die sich evtl. erweitern, verdammt lästig. Versteh mich nicht falsch, ich hab auch jahrelang mit typisierten dateien gearbeitet, aber irgendwann sieht man ein, dass das mehr ärger als nutzen bringt...
und JA das geht schnell und einfach! Leite einfach eine klasse von TCollectionItem (TPatient) ab und leite eine klasse von TPersistent ab (TZustand). Alle daten die du speichern willst deklariest du als published properties und erzeugst im konstruktor des items die nötigen unterobjekte (TZustand und die stringlisten). Dann nimmst du die TmxJsCollection (dpCollection unit -> codeLib) und fütterst sie mit der item-klasse. dann collection.saveToFile/loadfromFile...und alles wird gut :stupid: |
Re: wie abspeichern?
Zitat:
Das Problem war nur, dass ich vorschnell in einem Zustand der völligen geistigen Umnachtung (Blackout) eine Antwort geschrieben habe. maximov hat Recht. Höre auf in Records zu Denken. Ich verwende Records nur noch wenn es nicht anders geht. Z.B. wenn API's Records als Parameter erwarten. P.S. Selbst hochgestellte Persönlichkeiten des öffentlichen Lebens hatten schon das eine oder andere Blackout. Da kann ich mir so ein kleines bestimmt leisten. |
Re: wie abspeichern?
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo glkbkk,
ich habe Dir mal ein kleines Beispiel zusammengebastelt. Hier ist die Umsetzung der Records in Klassen bzw. in Nachfahren von TPersistent und TCollectionItem:
Delphi-Quellcode:
unit patient_impl;
interface uses SysUtils, classes; Type TZustand = class(TPersistent) private FAtem: Integer; FHerzschlag: Integer; public procedure Assign(Source : TPersistent); override; // Sollte immer überschrieben werden published property Atem : Integer read FAtem write FAtem default 0; property Herzschlag : Integer read FHerzschlag write FHerzschlag default 0; end; TPatient = class(TCollectionItem) private FLaufText : String; FName : String; FTipps : TStrings; FStory : TStrings; FZustand : TZustand; procedure SetStory(const Value: TStrings); procedure SetTipps(const Value: TStrings); procedure SetZustand(const Value: TZustand); public constructor Create(Collection : TCollection); override; destructor Destroy; override; procedure Assign(Source : TPersistent); override; // Sollte immer überschrieben werden published property Name : String read FName write FName; property LaufText : String read FLaufText write FLaufText; property Story : TStrings read FStory write SetStory; property Tipps : TStrings read FTipps write SetTipps; property Zustand : TZustand read FZustand write SetZustand; end; implementation { TZustand } procedure TZustand.Assign(Source: TPersistent); begin If Source is TZustand then begin FAtem:=TZustand(Source).Atem; FHerzschlag:=TZustand(Source).Herzschlag; end; inherited Assign(Source); end; { TPatient } procedure TPatient.Assign(Source: TPersistent); begin If Source is TPatient then begin FLaufText:=TPatient(Source).LaufText; FName:=TPatient(Source).Name; FTipps.Assign(TPatient(Source).Tipps); FStory.Assign(TPatient(Source).Story); FZustand.Assign(TPatient(Source).Zustand); end; inherited Assign(Source); end; constructor TPatient.Create(Collection: TCollection); begin inherited Create(Collection); FZustand:=TZustand.Create; FTipps:=TStringList.Create; FStory:=TStringList.Create; end; destructor TPatient.Destroy; begin FStory.Free; FTipps.Free; FZustand.Free; inherited Destroy; end; procedure TPatient.SetStory(const Value: TStrings); begin FStory.Assign(Value); end; procedure TPatient.SetTipps(const Value: TStrings); begin FTipps.Assign(Value); end; procedure TPatient.SetZustand(const Value: TZustand); begin FZustand.Assign(Value); end; end. |
Re: wie abspeichern?
Moin.
Darfs vielleicht noch ein template sein? Womit die benutzung entgültig einfacher nicht geht und tausendmal praktischer als irgend ein array ist (zur funktionsweise der templates siehe links im code):
Delphi-Quellcode:
Und einbinden kannst du es so:
{$IFNDEF TYPED_DP_COLLECTION_TEMPLATE}
unit dpCollection_tmpl; // written by MaxHub (maximov) 10.07.2004 // dpCollection: [url]http://www.delphipraxis.net/topic28945_tcollection+und+tcollectionitem.html[/url] // thanks to Thomas Mueller for his 'Object Pascal Templates' article // -> [url]http://www.dummzeuch.de/delphi/object_pascal_templates/deutsch.html[/url] // thanks to Rossen Assenov for the original narticle 'Templates in Object Pascal' // -> [url]http://community.borland.com/article/0,1410,27603,00.html[/url] interface uses Classes, dpCollection; type _COLLECTION_ITEM_ = TCollectionItem; {$ENDIF TYPED_DP_COLLECTION_TEMPLATE} {$IFNDEF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS} type _COLLECTION_ = class (TmxJsCollection) protected function GetItem (const aIndex : Integer) : _COLLECTION_ITEM_; procedure SetItem (const aIndex : Integer; const aValue : _COLLECTION_ITEM_); public constructor Create; function Add : _COLLECTION_ITEM_; function FindItemID (const aID : Integer) : _COLLECTION_ITEM_; function Insert (const aIndex : Integer) : _COLLECTION_ITEM_; property Items [const aIndex : Integer] : _COLLECTION_ITEM_ read GetItem write SetItem; end; {$ENDIF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS} {$IFNDEF TYPED_DP_COLLECTION_TEMPLATE} implementation {$DEFINE TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS} {$ENDIF TYPED_DP_COLLECTION_TEMPLATE} {$IFDEF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS} { TYPED_DP_COLLECTION } constructor _COLLECTION_.Create; begin inherited Create(_COLLECTION_ITEM_); end; function _COLLECTION_.Add : _COLLECTION_ITEM_; begin Result := _COLLECTION_ITEM_ (inherited Add); end; function _COLLECTION_.FindItemID (const aID : Integer) : _COLLECTION_ITEM_; begin Result := _COLLECTION_ITEM_ (inherited FindItemID (aID)); end; function _COLLECTION_.GetItem (const aIndex : Integer) : _COLLECTION_ITEM_; begin Result := _COLLECTION_ITEM_ (inherited GetItem (aIndex)); end; function _COLLECTION_.Insert (const aIndex : Integer) : _COLLECTION_ITEM_; begin Result := _COLLECTION_ITEM_ (inherited Insert (aIndex)); end; procedure _COLLECTION_.SetItem (const aIndex : Integer; const aValue : _COLLECTION_ITEM_); begin inherited SetItem (aIndex, aValue); end; {$WARNINGS off} {$IFNDEF TYPED_DP_COLLECTION_TEMPLATE} end. {$ENDIF TYPED_DP_COLLECTION_TEMPLATE} {$ENDIF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS} {$DEFINE TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS}
Delphi-Quellcode:
mit der collection TPatienten kannst du kann alles machen und musst noch nicht mal casten :wink:
interface
uses dpCollection, SysUtils, classes; {!!! Hier die definitionen von jens -> TPatient etc. !!!} {$define TYPED_DP_COLLECTION_TEMPLATE} type _COLLECTION_ITEM_ = TPatient; // typ der collectionItems festlegen {$INCLUDE 'dpCollection_tmpl.pas'} // typisierte collection generieren TPatienten = _COLLECTION_; // fertige collection eine sprechenden namen geben ... |
Re: wie abspeichern?
hmmm :gruebel: :gruebel:
den ersten code versteh ich eigentlich aber den zweiten.... tut der dasselbe? ich werd wohl ma den ersten nutzen.... thx :gruebel: |
Re: wie abspeichern?
Zitat:
Ich bin mir nicht wirklich sicher, ob du das alles hier zu schätzen weisst. Naja egal...am besten du liest dir nochmal alles gut durch und vor allem die vielen kleinen anmerkungen, links und kommentare. Damit man nicht immer alles zweimal sagen muss :mrgreen: |
Re: wie abspeichern?
vielleicht weiss ich es zu schätzen wenn ichs verstanden hab :wink:
ich wär euch erst m,al schon dankbar genug wenns einfach nur funzt.... thx :thuimb: |
Re: wie abspeichern?
OK! Zum mitschreiben :-D
Delphi-Quellcode:
...ok. hatte das include im implementation-teil vergessen. Das hättest du aber schnell gesehen, wenn du dir mal die links angesehen hättest. Schwamm drüber. Funktionert super! Und zwar so:
unit patient_impl;
interface uses dpCollection, SysUtils, classes; Type TZustand = class(TPersistent) private FAtem: Integer; FHerzschlag: Integer; public procedure Assign(Source : TPersistent); override; // Sollte immer überschrieben werden published property Atem : Integer read FAtem write FAtem default 0; property Herzschlag : Integer read FHerzschlag write FHerzschlag default 0; end; TPatient = class(TCollectionItem) private FLaufText : String; FName : String; FTipps : TStrings; FStory : TStrings; FZustand : TZustand; procedure SetStory(const Value: TStrings); procedure SetTipps(const Value: TStrings); procedure SetZustand(const Value: TZustand); public constructor Create(Collection : TCollection); override; destructor Destroy; override; procedure Assign(Source : TPersistent); override; // Sollte immer überschrieben werden published property Name : String read FName write FName; property LaufText : String read FLaufText write FLaufText; property Story : TStrings read FStory write SetStory; property Tipps : TStrings read FTipps write SetTipps; property Zustand : TZustand read FZustand write SetZustand; end; // mit dem einbinden des templates erzeugst du eine collection, // die auf deine eigene item-klasse spezialisiert ist. // Du musst nix mehr casten!!! {$define TYPED_DP_COLLECTION_TEMPLATE} type _COLLECTION_ITEM_ = TPatient; // typ der collectionItems festlegen {$INCLUDE 'dpCollection_tmpl.pas'} // typisierte collection generieren TPatienten = _COLLECTION_; // fertige collection eine sprechenden namen geben implementation {$INCLUDE dpCollection_tmpl.pas} { TZustand } procedure TZustand.Assign(Source: TPersistent); begin If Source is TZustand then begin FAtem:=TZustand(Source).Atem; FHerzschlag:=TZustand(Source).Herzschlag; end; inherited Assign(Source); end; { TPatient } procedure TPatient.Assign(Source: TPersistent); begin If Source is TPatient then begin FLaufText:=TPatient(Source).LaufText; FName:=TPatient(Source).Name; FTipps.Assign(TPatient(Source).Tipps); FStory.Assign(TPatient(Source).Story); FZustand.Assign(TPatient(Source).Zustand); end; inherited Assign(Source); end; constructor TPatient.Create(Collection: TCollection); begin inherited Create(Collection); FZustand:=TZustand.Create; FTipps:=TStringList.Create; FStory:=TStringList.Create; end; destructor TPatient.Destroy; begin FStory.Free; FTipps.Free; FZustand.Free; inherited Destroy; end; procedure TPatient.SetStory(const Value: TStrings); begin FStory.Assign(Value); end; procedure TPatient.SetTipps(const Value: TStrings); begin FTipps.Assign(Value); end; procedure TPatient.SetZustand(const Value: TZustand); begin FZustand.Assign(Value); end; end.
Delphi-Quellcode:
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, patient_impl; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } collection:TPatienten; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin collection:=TPatienten.Create; with collection.Add do begin Name := 'horst'; LaufText := '...'; Zustand.Atem := 42; Zustand.Herzschlag := 180; end; end; procedure TForm1.FormDestroy(Sender: TObject); begin collection.saveTiFile('c:/patienten.db.txt.dfm'); collection.Free; end; end. hoffe jetzt wirds klarer? |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:22 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 by Thomas Breitkreuz