Autor Beitrag
Coder
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 08.06.06 22:07 
Hi
Ich arbeite an einem VirtualTreeView in verbindung mit Objekten.
Hab mich mit Tutorials und Google schlau gemacht und folgenden Source zusammen gebastelt.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
type
  TNode = class
    FText: String;
  end;

  PTreeData = ^TTreeData;
  TTreeData = record
    FObject: TObject;
  end;

...

procedure TForm1.FormCreate(Sender: TObject);
var
  NodeData: PTreeData;
begin
  VST.NodeDataSize := SizeOf(TTreeData);
  NodeData := VST.GetNodeData(VST.GetFirst);
  NodeData.FObject := TNode.Create;
  TNode(NodeData.FObject).FText := 'Test!';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NodeData: PTreeData;
begin
  NodeData := VST.GetNodeData(VST.GetFirst);
  ShowMessage(TNode(NodeData.FObject).FText);
end;

Der funktioniert soweit gut. :D
(Ok ich muss am Ende noch den Speicher freigeben)

Nun wollt ich den etwas kürzen.
Ohne den Umweg über TTreeData.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
type
  PNode = ^TNode;
  TNode = class
    FText: String;
  end;

...

procedure TForm1.FormCreate(Sender: TObject);
var
  NodeData: PNode;
begin
  VST.NodeDataSize := SizeOf(TNode);
  NodeData := PNode(VST.GetNodeData(VST.GetFirst));
  NodeData := PNode(TNode.Create);
  TNode(NodeData).FText := 'Test!';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NodeData: PNode;
begin
  NodeData := PNode(VST.GetNodeData(VST.GetFirst)^);
  ShowMessage(TNode(NodeData).FText);
end;


Doch bei Klick auf den Button ernte ich eine Zugriffsverletzung.
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Fr 09.06.06 22:50 
Hm keiner ne Idee?
Ok, dann werd ich wohl Ersteres nehmen müssen. :gruebel:
Aber wieso geht das zweite eigentlich nich? :lupe:
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Fr 09.06.06 23:20 
Woran hängts denn?

Wäre es theoretisch nicht möglich statt
ausblenden Delphi-Quelltext
1:
NodeData := PNode(TNode.Create);					


einfach
ausblenden Delphi-Quelltext
1:
NodeData := TNode.Create;					

zu schreiben? Zurück kommt von Create nur ein Pointer..
Das ist jetzt ein Tipp ins blaue (VST ist grad bei mir net installiert). Hoffe es geht

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Fr 09.06.06 23:55 
Nein dann sagt er dort:
[Fehler] Unit1.pas(44): E2010 Inkompatible Typen: 'PNode' und 'TNode'.

Wenn ich alles über TNode mache kommt auch wieder ne Zugriffsverletzung bei
ausblenden Delphi-Quelltext
1:
ShowMessage(TNode(NodeData).FText);					


Ich blick da irgendwie nich so ganz durch. :(
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Sa 10.06.06 19:40 
Geht das?
ausblenden Delphi-Quelltext
1:
NodeData := @TNode.Create;					


Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Sa 10.06.06 20:37 
user profile iconalias5000 hat folgendes geschrieben:
Geht das?
ausblenden Delphi-Quelltext
1:
NodeData := @TNode.Create;					
Dachte ich auch.
Aber dann kommt eine Zugriffsverletzung beim darauffolgenden: TNode(NodeData).FText := 'Test!';.

Scheinbar ist es wirklich unlösbar.

Danke trotzdem! :D
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: So 11.06.06 15:25 
Hab jetzt den Source so umgeschrieben das sich die Daten leichter ändern lassen.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
type
  TNode = class
    FText: String;
  end;

  PTreeData = ^TTreeData;
  TTreeData = record
    FObject: TObject;
  end;

  TForm1 = class(TForm)
    ...
    function GetData(Node: PVirtualNode): TNode;

var
  Data: TNode;
  TreeData: PTreeData;


function TForm1.GetData(Node: PVirtualNode): TNode;
begin
  TreeData := VST.GetNodeData(Node);
  Result := TNode(TreeData.FObject);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  VST.NodeDataSize := SizeOf(TTreeData);

  Data := GetData(VST.GetFirst);
  Data.FText := 'Test!';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Data := GetData(VST.GetFirst);
  ShowMessage(Data.FText);
end;


procedure TForm1.VSTInitNode(Sender: TBaseVirtualTree; ParentNode,
  Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
begin
  TreeData := VST.GetNodeData(Node);
  TreeData.FObject := TNode.Create;
end;

procedure TForm1.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
  TreeData := VST.GetNodeData(Node);
  TreeData.FObject.Free;
end;

Funktioniert! :D

So jetzt soll GetData natürlich OOP werden.
(berichtigter Source unten)


Zuletzt bearbeitet von Coder am So 11.06.06 19:06, insgesamt 3-mal bearbeitet
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: So 11.06.06 19:01 
So habs nun geschafft das ganze OOP zu machen. :D
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
type
  TNode = class
    FText: String;
    function GetData(Node: PVirtualNode): TNode;
  end;

  PTreeData = ^TTreeData;
  TTreeData = record
    FObject: TObject;
  end;

var
  Data: TNode;
  TreeData: PTreeData;


function TNode.GetData(Node: PVirtualNode): TNode;
begin
  TreeData := Form1.VST.GetNodeData(Node);
  GetData := TNode(TreeData.FObject);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  VST.NodeDataSize := SizeOf(TTreeData);

  Data := Data.GetData(VST.GetFirst);
  Data.FText := 'Test!';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Data := Data.GetData(VST.GetFirst);
  ShowMessage(Data.FText);
end;

procedure TForm1.VSTInitNode(Sender: TBaseVirtualTree; ParentNode,
  Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
begin
  TreeData := VST.GetNodeData(Node);
  TreeData.FObject := TNode.Create;
end;

procedure TForm1.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
  TreeData := VST.GetNodeData(Node);
  TreeData.FObject.Free;
end;


Da diese Version aber länger ist und OOP für GetData keinen Sinn macht werde ich die Version aus meinem vorigen Post benutzen.

Daaankee!!! :D :wink2:
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: So 11.06.06 19:48 
user profile iconCoder hat folgendes geschrieben:
user profile iconalias5000 hat folgendes geschrieben:
Geht das?
ausblenden Delphi-Quelltext
1:
NodeData := @TNode.Create;					
Dachte ich auch.
Aber dann kommt eine Zugriffsverletzung beim darauffolgenden: TNode(NodeData).FText := 'Test!';.

Das kann nicht gehen, weil das nur ein Zeiger auf einen Zeiger ist. Richtig wäre IMHO TNode(NodeData^).FText := 'Test!';

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: So 11.06.06 20:00 
Wie meinst dus genau?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
type
  PNode = ^TNode;
  TNode = class
    FText: String;
  end;


procedure TForm1.FormCreate(Sender: TObject);
var
  NodeData: PNode;
begin
  VST.NodeDataSize := SizeOf(TNode);
  NodeData := PNode(VST.GetNodeData(VST.GetFirst));
  NodeData := PNode(TNode.Create);
  TNode(NodeData^).FText := 'Test!';  //Zugriffsverletzung
end;


Trotzdem Danke.
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Di 13.06.06 20:16 
Hab nun noch mal die VST-Hilfe gewällzt und kleine Verbesserungen vorgenommen.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
type
  TNode = class
    FText: String;
  end;

  PTreeData = ^TTreeData;
  TTreeData = record
    FObject: TObject;
  end;

    function GetData(Node: PVirtualNode): TNode;

var
  Data: TNode;

procedure TForm1.VSTInitNode(Sender: TBaseVirtualTree; ParentNode,
  Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
begin
  PTreeData(VST.GetNodeData(Node)).FObject := TNode.Create;
end;

procedure TForm1.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
  PTreeData(VST.GetNodeData(Node)).FObject.Free;
end;

function TForm1.GetData(Node: PVirtualNode): TNode;
begin
  Result := TNode(PTreeData(VST.GetNodeData(Node)).FObject);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  VST.NodeDataSize := SizeOf(TTreeData);

  Data := GetData(VST.GetFirst);
  Data.FText := 'Test!';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Data := GetData(VST.GetFirst);
  ShowMessage(Data.FText);
end;

Funktioniert besstens! :D
Danke nochmal

MfG Coder