not completely sure what youre trying to do but instead of accumulating 
the count in a global variable, how about making this a function that 
returns the count of all children under the caller? that way the 
recursion handles the accumulation without a seperate variable.

function tsGrpExtractNodes(grp: tsxGROUP):integer;
var
  pChildNode: tsxSOBJ;
  objtype: tsxSOBJTYPE;
  i:integer;
begin
  pChildNode:= tsxGNodeGetFirstChild(grp);
  while (pChildNode <> nil) do begin
    objtype:= tsxSobjGetType(pChildNode);
    if ((objtype = e_tsxGROUP) or (objtype = e_tsxIKGROUP)) then begin
      //may have children
      Inc(Result,tsGrpExtractNodes(pChildNode));
    end
    else begin
      //has no children, just something to be counted
      Inc(Result);
    end;
    pChildNode:= tsxGNodeGetNext(pChildNode);
  end;
end;

i dont get why theres an assignment to the nodes array in the middle of 
this, so maybe i dont get what youre doing here. it looks like the 
nodes:PObjArray array is being changed by the counting process.. that 
cant be a good idea. or.. i dont get it (but i am quite dense). anyway i 
hope this illustrated the core concept of avoiding a seperate global 
accumulator variable by passing the local count back "up" throught the 
return var (Result) of the function.

[EMAIL PROTECTED] wrote:

>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
>  
>
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to