[Lazarus] VirtualTreeView, Absoluteindex example on freepascal.org

Frederic Da Vitoria davitofrg at gmail.com
Thu Sep 12 14:50:51 CEST 2013


2013/9/11 Jürgen Hestermann <juergen.hestermann at gmx.de>

> On
> http://wiki.freepascal.org/**VirtualTreeview_Example_for_**Lazarus<http://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus>
> there is the following example procedure:
>
> ------------------------------**------------------
> procedure TForm1.Button1Click(Sender: TObject);
> Var
>   Data: PTreeData;
>   XNode: PVirtualNode;
>   Rand: Integer;
> Begin
>   Randomize;
>   Rand := Random(99);
>   XNode:=VST.AddChild(nil);
>
>   if VST.AbsoluteIndex(XNode) > -1 then
>   Begin
>    Data := VST.GetNodeData(Xnode);
>    Data^.Column0:= 'One ' + IntToStr(Rand);
>    Data^.Column1:= 'Two ' + IntToStr(Rand + 10);
>    Data^.Column2:= 'Three ' + IntToStr(Rand - 10);
>   End;
> End;
> ------------------------------**------------------
>
> Can someone explain what the line
>
>   if VST.AbsoluteIndex(XNode) > -1 then
>
> actually means?
> The method AbsoluteIndex gives back a Cardinal so how can it be less than
> 0?
> Therefore the if statement is completely useless because it is always true.
>
> BTW: I was pointed to this because Lazarus (or FPC?) showed me a compiling
> message that this line will cause a type convertion to 64 bit (well done!).
>

I didn't look at the code, but usually index = -1 means that the item was
not found. So maybe AbsoluteIndex's result should actually be signed? In
this example, I guess the test is to avoid a bug if AddChild does not
manage to insert the root node. I don't know if AddChild could create a
node without inserting, but this is what the code seems to be testing.

-- 
Frederic Da Vitoria
(davitof)

Membre de l'April - « promouvoir et défendre le logiciel libre » -
http://www.april.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20130912/25845b1a/attachment-0003.html>


More information about the Lazarus mailing list