Autor Beitrag
g1o2k4
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 493



BeitragVerfasst: Do 25.12.08 01:54 
nabend

es geht um folgendes:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
  TData = record
    a: double;
    b: integer;
    c: char;
    end;

  TKlasse = class
  private
    Werte: array of TData;
  public

  published

  end;


ich möchte, dass der klassenbenutzer den inhalt des arrays Werte lesen, aber nicht überschreiben kann. nun ist das mit der übergabe von dynamischen arrays leider nicht so einfach. bzw der aufwand dafür scheint mir nicht gerechtfertigt.
besser fände ich, wenn man ein array was man nur lesen kann als puplic deklariert, in das man dann das Werte array reinkopiert.
kann man const variablen zur laufzeit deklarieren ? oder ist const nur dazu da, dass der compiler weiß, dass niemand darauf zugreifen kann ?

fällt vielleicht jemandem ne andere einfache lösung für die übergabe/das lesen des Werte arrays ein ?.


Zuletzt bearbeitet von g1o2k4 am Do 25.12.08 03:17, insgesamt 1-mal bearbeitet
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 25.12.08 01:57 
Zwei Wege:
1. Array über Read-Only-Property rausreichen
2. Array als Konstante deklarieren, über ein wenig Pointer-Fun initialisieren und dann diese Konstante weiterreichen ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
g1o2k4 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 493



BeitragVerfasst: Do 25.12.08 02:53 
zu read-only-property fällt mir jetzt nur sowas ein:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
 ...
 
  TKlasse = class  
  private  
    Werte: array of TData;  
  public  
    property GetWerte: array of TData read Werte;
 
  published  

  end;

meintest du das oder ist read-only-property was anderes ?
der code funktioniert nicht, d.h. der compiler meckert über das "array" nach ':' in der public section. falls das die read-only-property ist, kann ichs wohl vergessen.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 25.12.08 02:59 
Du kannst auch Array-Properties in Delphi deklarieren.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
g1o2k4 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 493



BeitragVerfasst: Do 25.12.08 03:07 
aber damit kann ich doch nur immer ein element auslesen oder ?
ich brauche aber das gesamte array.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Do 25.12.08 03:10 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
type
  TDataArray: Array of TData;

  [...]

  property Blup: TDataArray read Blap;


T(d)ada :mrgreen:

Anmk.: Etwas mehr muss man noch tun. Entsprechende Setter und Getter könnten gebraucht werden.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
g1o2k4 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 493



BeitragVerfasst: Do 25.12.08 03:15 
na toll. da sieht man wieder was delphi für eine explizite sprache ist...
genau den ansatz hatte ich mir als erstes gedacht und folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
  TAoData = array of TData;

  TKlasse = class
  private
    Werte: array of TData;

  public
    property GetWerte: TAoData read Werte;
  published
    constructor Create();
  end;


das klappte nicht, aber mit
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
  TAoData = array of TData;

  TKlasse = class
  private
    Werte: TAoData;

  public
    property GetWerte: TAoData read Werte;
  published
    constructor Create();
  end;


geht es logischerweise. 2 uhr nachts ist >>bei mir<< einfach nicht mehr die zeit, um auf solche kleinigkeiten zu achten.

thx für den denkanstoß, das hab ich gebraucht!

edit: get und set funktionen brauch ich nichtmal.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 25.12.08 04:03 
user profile icong1o2k4 hat folgendes geschrieben Zum zitierten Posting springen:
edit: get und set funktionen brauch ich nichtmal.

Dann kann man mit deinem Object die Daten aber ändern, weil man den Getter umgehen kann ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
g1o2k4 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 493



BeitragVerfasst: Do 25.12.08 15:44 
wie das ?

sollte ich dann besser folgendes machen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
  TAoData = array of TData;  

  TKlasse = class  
  private  
    Werte: TAoData;  
    function GetWerte():TAoData;
  public  
    property GetWerte: TAoData read GetWerte;  
  published  
    constructor Create();  
  end

implementation
...
function GetWerte():TAoData;
begin
 result := Werte;
end;


oder das:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
  TAoData = array of TData;  

  TKlasse = class  
  private  
    Werte: TAoData;  
    
  public  
    function GetWerte():TAoData; 
  published  
    constructor Create();  
  end

implementation
...
function GetWerte():TAoData;
begin
 result := Werte;
end;


wäre doch im prinzip dasselbe !? ich dachte propertys wären dazu gut um den zugriff direkt bereitszustellen bzw zu verhindern, ohne es noch in funktionen zu kapseln.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 25.12.08 20:05 
Ich hab auf einen Hack angespielt, mit dem man ReadOnly-Properties schreiben kann, die direkt auf ein Feld verweisen. Siehe www.swissdelphicente.../showcode.php?id=665

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
turboPASCAL
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP / Vischda
D6 PE / D2005 PE
BeitragVerfasst: Do 25.12.08 21:24 
"Konstante Variablen" Was 'sn dat ? :gruebel:

_________________
Nein, ich bin nicht der turboPASCAL aus der DP, ich seh nur so aus... :P
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Do 25.12.08 21:28 
Zitat:
"Konstante Variablen" Was 'sn dat ? :gruebel:
Noch nie im Stehen gerannt? :mrgreen:

_________________
Markus Kinzler.
turboPASCAL
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP / Vischda
D6 PE / D2005 PE
BeitragVerfasst: Do 25.12.08 21:56 
user profile iconmkinzler hat folgendes geschrieben Zum zitierten Posting springen:
Noch nie im Stehen gerannt? :mrgreen:

:lol:

_________________
Nein, ich bin nicht der turboPASCAL aus der DP, ich seh nur so aus... :P
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Do 25.12.08 22:02 
user profile icong1o2k4 hat folgendes geschrieben Zum zitierten Posting springen:
ich dachte propertys wären dazu gut um den zugriff direkt bereitszustellen bzw zu verhindern, ohne es noch in funktionen zu kapseln.
Das Array kann auch nicht verändern, den Inhalt schon. Die Property liefert ja nur den Zeiger auf das Array, und dieser Zeiger kann nicht verändert werden.

Du kannst das dynamisch Array als Eigenschaft einer Klasse nicht 'read only' machen. Das bietet Delphi nicht. Du kannst nur den Zugriff auf die einzelnen Elemente eines Arrays über Array-Properties schützen. Dann kannst Du aber das dynamische Array in Gänze nicht als Read-Only Eigenschaft zur Verfügung stellen und erwarten, das die Elemente auch nicht verändert werden können.

Vielleicht geht es mit statischen Arrays.

_________________
Na denn, dann. Bis dann, denn.