[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

find a node in VST

i am using this function to find node in treeview

function TFORM1.lookingTreeView(name: String): PVirtualNode;
var
Node: PVirtualNode;
Data: PUserData;
begin
Result := nil;
Node := VDT1.GetFirst;
while ((Node <> nil) and (Result = nil)) do
begin
Data := VDT1.GetNodeData(Node);
if (Data.FObject.name= name) then
begin
Result := Node;
break;
end
else
Node := VDT1.GetNext(Node);
end;
end;

Open in new window


and i use it like so
Node := lookingTreeView(namedata);

if Node <> nil then
begin
// then stop execute the code 
exit;
end;
// if its nil then do execute the rest of code

Open in new window


so this code search for node in Vst and if the node exits stop the code execution and if does not exits do the rest  

but some times this function got bypassed i mean when a node is already exits the if statement of node <> nil does not detect that its not nil and continue executing the code.
0
dolphin King
Asked:
dolphin King
  • 2
1 Solution
 
Geert GruwezOracle dbaCommented:
VST ?
Very Strong Tree ?
or a Virtually Silly Teacup ?
It helps in being explicit of what component  you use ...

don't like indenting ?
i use it to find memory leaks ...
never bothered about that ?
it also helps to make code more readable

Using SameText to search case insensitive

you got this code here: ?
http://www.delphipages.com/forum/showthread.php?t=152084

no point in checking for (Result = nil) in the while loop
That never happens

If you add the tree as a parameter, you can use this function from a common unit

function lookingTreeView(vst: VirtualStringTree; name: String): PVirtualNode;
var
  Node: PVirtualNode;
  Data: PUserData;
begin
  Result := nil;
  Node := vst.GetFirst;
  while Node <> nil do
  begin
    Data := vst.GetNodeData(Node);
    if SameText(Data.FObject.name, name) then
    begin
      Result := Node;
      break;
    end else
      Node := vst.GetNext(Node);
  end;
end;

Open in new window

0
 
Geert GruwezOracle dbaCommented:
an exit in the middle of a procedure ?
why not reverse the logic ?

begin
  Node := lookingTreeView(namedata);
  if Node = nil then
  begin
    // if its nil then do execute the rest of code
  end;
end;

Open in new window

1

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now