Hi Richard,
Yes, I would do as you suggest. Pass grpNodeCount as an extra parameter to
the function.
An alternative is to nest the whole recursive procedure inside another
procedure that has grpNodeCount as a local variable. This may be quicker as
it saves passing the value in at each call to the function. Maybe one of the
gurus out there might like to comment on this.
procedure findNodes( .....
var
grpNodecount : integer;
procedure tsGrpExtractNodes(grp: tsxGROUP; var nodes: PObjArray);
... your procedure
begin
grpNodeCount := 0;
tsGrpExtractNodes(grp,nodes);
end;
---Liz
Message: 4
Date: Wed, 14 Dec 2005 18:31:43 -0800
From: "Richard R" <[EMAIL PROTECTED]>
Subject: recursive function help
To: "'Delphi-Talk Discussion List'" <[email protected]>
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original
Hello. This isn't really necessary but we all know that using global
variables isnt a good convention. But in same cases it can't be helped. Here
is such a case. What this recursive function does is extracts all the child
objects that are grouped together as a single object. The single object
itself is not a real object but rather treated as one. So there is no parent
except for the group itself. Now these objects could be a sibling or a child
and thus are in the heirachy in a certain way. With this function, it doesnt
matter, it checks and gets them anyway. What I need help with is to find a
way to make grpNodeCount local instead of global. If I made grpNodeCount
local, it is always reinitialized to 0 no matter what I do, so I had to make
it global to keep its value. I need to initialize it 0 before and after a
call to this function I just thought of something, what if I made
grpNodeCount a variable in the function itself? That way it would keep it's
value and keep it local. What do you think?
// tsxGNode, tsxGroup, etc are all pointers to empty structures
PObjArray = array of tsxGNODE;
procedure tsGrpExtractNodes(grp: tsxGROUP; var nodes: PObjArray);
var
pChildNode: tsxSOBJ;
objtype: tsxSOBJTYPE;
begin
pChildNode:= tsxGNodeGetFirstChild(grp);
while (pChildNode <> nil) do begin
objtype:= tsxSobjGetType(pChildNode);
if ((objtype = e_tsxGROUP) or (objtype = e_tsxIKGROUP)) then begin
grp:= pChildNode;
tsGrpExtractNodes(grp, nodes);
end
else begin
nodes[grpNodeCount]:= pChildNode;
grpNodeCount:= grpNodeCount + 1;
end;
pChildNode:= tsxGNodeGetNext(pChildNode);
end;
end;
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk