Board index » delphi » Help : tList.IndexOf

Help : tList.IndexOf

I appreciate if someone could highlight where I have go wrong.

I wanted to check if a record is in a tlist. If it is not there,
insert that record into the list.

type
  MyTyp = record
                    Field1,
                    Field2 : string[10];
                 end;
var
  MyList : tList;
  MyRec : ^MyTyp;

........
.......
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  new(MyRec);
with MyRec do
  begin
      Field1 := 'Testing 123';
      Field2 := 'Testing XYZ';
  end;
  if MyList.IndexOf(MyRec) = -1 then
    MyList.Add(MyRec);
end;

The above causes the same record value inserted every time button is
clicked.

Where have I gone wrong?

Thanks in advance.

 

Re:Help : tList.IndexOf


lo...@pc.jaring.my (Loo, Wah Kong) wrote:

Quote
>I appreciate if someone could highlight where I have go wrong.
>I wanted to check if a record is in a tlist. If it is not there,
>insert that record into the list.
>type
>  MyTyp = record
>                    Field1,
>                    Field2 : string[10];
>                 end;
>var
>  MyList : tList;
>  MyRec : ^MyTyp;
>........
>.......
>procedure TForm1.BitBtn1Click(Sender: TObject);
>begin
>  new(MyRec);

ok, points to different heap place each time
Quote
>with MyRec do

 with MyRec^ do {your compiler must have mentioned it}

Quote
>  begin
>      Field1 := 'Testing 123';

                  1234567890^--oops, string[10]
Quote
>      Field2 := 'Testing XYZ';
>  end;
>  if MyList.IndexOf(MyRec) = -1 then

TList.IndexOf does not know about your fields, only that
the new _pointers_ it keeps getting are different from any
added before. Since there's no pointer match, -1 is returned.

Quote
>    MyList.Add(MyRec);

hence the new and different pointer is added, even though it
points to identical values.

Quote
>end;
>The above causes the same record value inserted every time button is
>clicked.
>Where have I gone wrong?
>Thanks in advance.

If you want to insert pointers only when values don't match,
you have to program the comparison yourself. If you have a
large table, you may benefit from hash techniques. Otherwise
just brute force scan the list every time. And if you find a
match and don't add the new pointer, dispose of it.

Also dispose of each pointer in the list before you free it.

HTH. Regards, Bengt Richter

Re:Help : tList.IndexOf


MAybe you can't distuinguish between each "myrec"

Code modified below, which  may help if you run

HTH

Boris Ingram, Cyborg Software
boris...@iafrica.com
http://www.pcb.co.za/users/borising/cyborg.htm

In article <4eioq3$...@jaring.my> lo...@pc.jaring.my (Loo, Wah Kong) writes:

Quote
>From: lo...@pc.jaring.my (Loo, Wah Kong)
>Subject: Help : tList.IndexOf
>Date: Mon, 29 Jan 1996 15:35:11 GMT
>I appreciate if someone could highlight where I have go wrong.
>I wanted to check if a record is in a tlist. If it is not there,
>insert that record into the list.
>type
>  MyTyp = record
>                    Field1,
>                    Field2 : string[10];
>                 end;
>var
>  MyList : tList;
>  MyRec : ^MyTyp;

const
  clickcount : integer = 0;;

Quote
>........
>.......
>procedure TForm1.BitBtn1Click(Sender: TObject);
>begin
>  new(MyRec);

  inc(clickcount);

Quote
>with MyRec do
>  begin

      Field1 := 'Testing #'+inttostr(clickcount);
Quote
>      Field2 := 'Testing XYZ';
>  end;
>  if MyList.IndexOf(MyRec) = -1 then
>    MyList.Add(MyRec);
>end;
>The above causes the same record value inserted every time button is
>clicked.
>Where have I gone wrong?
>Thanks in advance.

Re:Help : tList.IndexOf


On Mon, 29 Jan 1996 15:35:11 GMT, lo...@pc.jaring.my (Loo, Wah Kong)
wrote:

Quote
>I appreciate if someone could highlight where I have go wrong.

>I wanted to check if a record is in a tlist. If it is not there,
>insert that record into the list.
...
>The above causes the same record value inserted every time button is
>clicked.

>Where have I gone wrong?

TList.IndexOf looks for an identical pointer value. It does not look
at what the pointer refers to. You need to write your own lookup
routine.
--
Ray Lischner         (li...@tempest-sw.com)
Tempest Software, Corvallis, Oregon, USA

Other Threads