Board index » delphi » Adding integers to TList

Adding integers to TList

I once saw an example of how to store integers in a TList.  Anyone care
to enlighten me one more time?

I relize I can make an object with 1  integer field, then store that
object on the list, but the approach I say had the integer stored
directly on the TList.

thanks

Joe

 

Re:Adding integers to TList


Quote
> I once saw an example of how to store integers in a TList.  Anyone care
> to enlighten me one more time?

> I relize I can make an object with 1  integer field, then store that
> object on the list, but the approach I say had the integer stored
> directly on the TList.

> thanks

> Joe

Would storing it as a string and retrieving it as an Integer work?

Like:

MyList.Add('1');
Tabset1.TabIndex := IntToStr(MyList[0]);

----
RKR

****************************************
E-Mail:  r...@primenet.com
****************************************

Re:Adding integers to TList


In article <32A72BA0.4...@sunshine.net>,
   Joe Nodeland <joe_nodel...@sunshine.net> wrote:
]-I once saw an example of how to store integers in a TList.  Anyone care
]-to enlighten me one more time?
]-
]-I relize I can make an object with 1  integer field, then store that
]-object on the list, but the approach I say had the integer stored
]-directly on the TList.
]-
]-thanks
]-
]-Joe

you know, I never would have thought of doing this...but with
a little bit of typecasting it can be done...will longints do
in place of integers?  (I'm using D1, if you have D2 it may not
be an issue...)

Program Duh;

uses WinCRT, Classes, SysUtils;

Var
  AList  : TList;
  X, n   : longint;
  IntPTR : Pointer;

BEGIN
  AList := NIL;
  X := 17;
{= a TList stores any old kind of pointer, so let's =}
{= pretend X is a pointer...                        =}
  IntPTR := Pointer(X);
  Try
    AList := TList.Create;
    AList.Add( IntPTR );
    for n := 2 to 5 do
      BEGIN
        X := 2*X;
        IntPTR := Pointer(X);
        AList.Add( IntPTR );
      END;
    writeln('Contents of the list : ');
{= now do the reverse...typecast the pointers that started =}
{= life as (long)integers back to their original form...   =}
    for n := 0 to AList.count-1 do
      writeln('    AList[', n, '] = ', longint(AList[n]):2);
  Except
    on E : Exception do
      writeln('[', X, ']  ', E.ClassName, ' :  ', E.message);
  END;
  AList.Free;
END.

is this the sort of thing you had in mind?

Mark Vaughan

Re:Adding integers to TList


Quote
Joe Nodeland wrote:

> I once saw an example of how to store integers in a TList.  Anyone care
> to enlighten me one more time?

> I relize I can make an object with 1  integer field, then store that
> object on the list, but the approach I say had the integer stored
> directly on the TList.

> thanks

> Joe

var
  i: integer;
...
  MyList.Add(pointer(i));
  ...
  i := integer(MyList[0]);

Chris

Re:Adding integers to TList


Quote
Chris Jobson wrote:

> Joe Nodeland wrote:

> > I once saw an example of how to store integers in a TList.  Anyone care
> > to enlighten me one more time?

> > I relize I can make an object with 1  integer field, then store that
> > object on the list, but the approach I say had the integer stored
> > directly on the TList.

> > thanks

> > Joe

> var
>   i: integer;
> ...
>   MyList.Add(pointer(i));
>   ...
>   i := integer(MyList[0]);

> Chris

Type casting is wonderful.  I keep forgetting integers are types.

Joe

Re:Adding integers to TList


Joe Nodeland <joe_nodel...@sunshine.net> seems to have typed:

Quote
>I once saw an example of how to store integers in a TList.  Anyone care
>to enlighten me one more time?

Sure.  The pointers are the same size as a 32-bit integer, so just
cast back and forth. (Danger, Will Robinson!  Unsafe in the long
run!).

e.g.  MyList[1]:=Pointer(3);  i:=LongInt(MyList[1]);

or code to that effect.

Evan Simpson

Re:Adding integers to TList


Joe Nodeland <joe_nodel...@sunshine.net> wrote in article
<32A72BA0.4...@sunshine.net>...

Quote
> I once saw an example of how to store integers in a TList.  Anyone care
> to enlighten me one more time?

> I relize I can make an object with 1  integer field, then store that
> object on the list, but the approach I say had the integer stored
> directly on the TList.

> thanks

> Joe

The method I have used looks like this:
 ...items.AddObject('Somestring', Pointer(100));
ie setting the value of 100 to the object associated with 'Somestring'. You
need to Type the integer as a Pointer.

and to get the value back:
 i := integer(Items.Objects[items.indexof('SomeString')]);
will return 100...

Hope that helps
Mike

Re:Adding integers to TList


Quote
> The method I have used looks like this:
>  ...items.AddObject('Somestring', Pointer(100));
> ie setting the value of 100 to the object associated with 'Somestring'. You
> need to Type the integer as a Pointer.

> and to get the value back:
>  i := integer(Items.Objects[items.indexof('SomeString')]);
> will return 100...

Well, almost. ;) The original guy who posted asked for a TList. So the
above line would really be:

        j := List.Add(Pointer(100));
        i := integer(List[j]);

Thanks, cheers,
+-----------------------------+------------------------------------+
| Adrian Cable [Doom Master!] | Internet: [MeGALiTH on IRC]        |
| Fidonet: 2:252/169.3        | aca...@sv.span.com                 |
+-----------------------------+------------------------------------+
| Latest ViRC '96 release: 0.80                                    |
| Get it from: http://www.megalith.co.uk/virc                      |
+------------------------------------------------------------------+

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.6.3i

mQBNAzICpWsAAAECAKELh2Zi1a+jD1Nj7VYloLg+uVimsRZHv5DgfCDOOo0nnXNq
KE19vjB1ZD4Rs1NTP8cQEjsnTwrHajgcZK8ecUkABRG0IUFkcmlhbiBDYWJsZSA8
YWNhYmxlQHN2LnNwYW4uY29tPokAVQMFEDICpWtqOBxkrx5xSQEBYjkB/jEcpWFp
y4t6XKL/zj34hfyCZYJKXQpuJlMWklywhu5OC2Z6WlsKeS6deKTyrW0GWw9hw6gS
qPdw+TcN9PSQBbE=
=6ioa
-----END PGP PUBLIC KEY BLOCK-----

Re:Adding integers to TList


Will this work for strings? ie: addobject('myname',pointer('brian'))
--
Brian C. Layman
Director of Software Development
SoftForms Development Corporation
http://www.softforms.com
BLay...@Softforms.com

Michael Ruane <mru...@itl.co.nz> wrote in article

Quote
> and to get the value back:
>  i := integer(Items.Objects[items.indexof('SomeString')]);
> will return 100...

Other Threads