Passing pointers to records
In Delphi 2010, I have a TreeView. The treeview has a data object defined as a record.
// Define the Data Object that will be associated with each Node
type
TNodeRec = record
rId: Integer;
rDate: TDateTime;
rSite: string;
rURL: string;
rRedirURL: string;
rTitle: string;
rCategory: String;
rURL_RtnCode: Integer;
rURL_RtnText: String
end;
I associate this data object with EACH node in the treeview, then I fill in the values. This works fine....
var
BuffData: ^TNodeRec; // The data object associated with each node;
URLNode: TTreeNode;
begin
...
// Create our New Data Object
new(BuffData);
BuffData^.rId := 0;
BuffData^.rDate := EDate;
BuffData^.rURL := ElString;
BuffData^.rRedirURL := '';
BuffData^.rTitle := ElText;
URLNode := TV1.Items.AddChildObject(nil, BuffData^.rTitle, BuffData);
My challenge is that from a buttonClick event, I need to pass this data object to another routine (in another UNIT) to modify it, and have the modified values available to the calling routine... This would normally be done via a VAR declaration, but I don't know that I need that, since I have declared this as a POINTER to a record.
Anyway, my ButtonClick event looks like this...
procedure TURLListForm.btnEvalSelectedClick(Sender: TObject);
var
SelNode : TTreeNode;
BuffData : TNodeRec;
begin
// verify there is a node selected
...
BuffData := TNodeRec(TV1.Selected.Data^);
WebEvaluate(BuffData);
ShowMessage(TNodeRec(TV1.Selected.Data^).rRedirURL);
end;
The problem is that the ShowMessage box is always empty, aka rRedirURL is not getting passed back like it would as a VAR declaration.
WebEvaluate is defined as
procedure WebEvaluate(Data1: TNodeRec);
begin
TNodeRec(Data1).rRedirURL := 'Test value'
ShowMessage('In WebEvaluate Routine:' + Data1.rRedirURL);
end
Within the WebEvaluate routine, Data1.rRedirURL has a value assigned. When I get back to the calling routine, the value is lost. I believe it has to do with how I am passing the Data1 record, but I don't know how to correct it... What am I doing wrong?
Thanks
If you are going to ask WebEvaluate
to modify its parameter it must be passed as var
. The way you write the procedure, the procedure takes a copy of the parameters value, and modifies that. Remember that modification to a parameter passed by value cannot be seen outside the function.
Change it to be like so:
procedure WebEvaluate(var Data1: TNodeRec);
begin
Data1.rRedirURL := 'Test value';
ShowMessage('In WebEvaluate Routine:' + Data1.rRedirURL);
end;
On top of that you are actually passing a copy of the record to WebEvaluate
. Look at this code:
var
BuffData : TNodeRec;
....
BuffData := TNodeRec(TV1.Selected.Data^);
Since BuffData
is a record, assignment to BuffData
takes a copy. But you need to modify the record stored in the tree view and so must pass that record, and not a copy.
So, remove the local variable BuffData
that serves no purpose. And call the procedure like this, passing the record stored in the tree view:
WebEvaluate(TNodeRec(TV1.Selected.Data^));
To summarise you made the same mistake twice. You made a copy of the record in a local variable. You then passed that to your procedure which made another copy.
链接地址: http://www.djcxy.com/p/34942.html上一篇: 如何影响用于Android / ARM目标的Delphi XEx代码生成?
下一篇: 将指针传递给记录