Autor Beitrag
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 17.08.06 11:32 
Hallo,

normalerweise sind ja propertys so aufgebaut:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
type
  TMyClass = class
  private
    FStr: String;
  public
    property Str: String read FStr;
  end;


Nun habe ich mir aber ein Record (ob Record oder Klasse dürfte ja egal sein ;) ) definiert, da ich einige Eigenschaften mehrmals in meiner eigen Klasse haben möchte, nur wie verhindert man, dass die Werte überschreiben werden können?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
type
  TMyPrefs = record
    ...
  end;

type
  TMyClass = class
  private
    FPrefs: TMyPrefs;
  public
    property Prefs: String read FPrefs; //funktioniert leider nicht so, da hier nur verhindetr wird, dass direkt die Property überschreiben wird (es werden also die eigentlichen Preferencen nicht geschützt :-( )
  end;


//EDIT: Eins sollte ich vlt. noch dazu sagen: Es bringt mir nix, das die Prefs-Propertys alle per read zu schützten, da in MyClass einmal die Prefs. beschreibar sein sollen und einmal nicht ;).

Grüße
Heiko
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 17.08.06 11:47 
Moin!

user profile iconHeiko hat folgendes geschrieben:
Nun habe ich mir aber ein Record (ob Record oder Klasse dürfte ja egal sein

Nein, denn records haben keine Transaktionskontrollmöglichkeit. ;) Mach eine Klasse mit readonly-Properties draus, dann klappts.

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Heiko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 17.08.06 11:52 
user profile iconHeiko hat folgendes geschrieben:
//EDIT: Eins sollte ich vlt. noch dazu sagen: Es bringt mir nix, das die Prefs-Propertys alle per read zu schützten, da in MyClass einmal die Prefs. beschreibar sein sollen und einmal nicht ;).


user profile iconNarses hat folgendes geschrieben:
user profile iconHeiko hat folgendes geschrieben:
Nun habe ich mir aber ein Record (ob Record oder Klasse dürfte ja egal sein

Nein, denn records haben keine Transaktionskontrollmöglichkeit. ;) Mach eine Klasse mit readonly-Properties draus, dann klappts.


Nun ja, mir gehts hier daraum, ob man verhindern kann, dass ich 2x die gleiche Klasse schreiben muss, nur mit unterschiedlichen Property-Eigenschaften(read/write) ;).

Grüße
Heiko
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 17.08.06 12:54 
Moin!

Wie so 2x schreiben? :gruebel: Einfach eine weitere property WriteProtect: Boolean einführen und in den Setter-Methoden auf den Wert reagieren. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Heiko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 17.08.06 13:22 
Ich weiß, aber ich mag solche Varianten nicht, weil die Irreführend sind ;). Da denkt man, dass er die Props gesetzt hat, es dann aber im endeffekt doch nicht gemacht hat.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 17.08.06 13:33 
Moin!

Dann stell doch einfach das Setzen im WriteProtected-Modus unter Exception-Strafe... :D

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Heiko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 17.08.06 14:53 
user profile iconNarses hat folgendes geschrieben:
Moin!

Dann stell doch einfach das Setzen im WriteProtected-Modus unter Exception-Strafe... :D

cu
Narses


Das wäre ja noch grausamer, dann werden die Benutzer bestraft, und nicht nur der Entwickler ;). Schade nur, dass Borland keine Compiler-Direktiven dafür anbietet :(.