Board index » delphi » How to store a number in TStringList?

How to store a number in TStringList?

TStringlist allows an object to be associated with an item. What I want
to do is just associating a number with the string item in the
Stringlist object. How to do it? Your help would be appreciated.

Alice

 

Re:How to store a number in TStringList?


Quote
In article <31AA05D3.1...@tuns.ca> ak...@ccn.cs.dal.ca writes:
>From: ak...@ccn.cs.dal.ca
>Subject: How to store a number in TStringList?
>Date: Mon, 27 May 1996 16:43:15 -0300
>TStringlist allows an object to be associated with an item. What I want
>to do is just associating a number with the string item in the
>Stringlist object. How to do it? Your help would be appreciated.
>Alice

quite a few ways i can think of

say you have

const
  anumber : double = 3.142;

alist: tstringlist;

1 - alist.add(floattostr(anumber));
2 - type
          tmynumber = class
      public
                ano : double;
          end;

      amynumber := tmynumber.create;
      amynumber.ano :=3.142;
      alist.addobject('ano', mynumber);

3 - alist.addobject('ano', tobject(@anumuber));

HTH

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

Re:How to store a number in TStringList?


Quote

>From: ak...@ccn.cs.dal.ca
>Subject: How to store a number in TStringList?
>Date: Mon, 27 May 1996 16:43:15 -0300
>TStringlist allows an object to be associated with an item. What I want
>to do is just associating a number with the string item in the
>Stringlist object. How to do it? Your help would be appreciated.

IMO, the easiest solution is to convert the number to a string.
You also (probably) want to have your stringlist of numbers sorted in
numerical order.  Thus, you must be careful to
  1. preserve the correct sequence for negative numbers (assuming you'll have
some), and
  2. preserve the implied right justification of the numbers.
  3. Handle decimal positions consistently.

These are sloppy problems!  One approach I've used which is easy is to add
a large number, such as 1000000, before converting to a string. e.g.,

const BigNumber = 1000000;
var n : longint;
begin
  n := -314326;
  MyStringList.Add(IntToSTR(n+BigNumber));

  later on, when you retrieve the number, you must remember to substract
the BigNumber.

This sloppy approach should only be used when YOU control the numbers (not
the user). For example, its best when you only use positive integers from 0
to 1000.  Then problems #1 and #3 above disappear.

Re:How to store a number in TStringList?


Quote
On Mon, 27 May 1996 16:43:15 -0300, ak...@ccn.cs.dal.ca wrote:
>TStringlist allows an object to be associated with an item. What I want
>to do is just associating a number with the string item in the
>Stringlist object. How to do it? Your help would be appreciated.

Just type cast the number to TObject and vice versa:

StringList.Objects[Index] := TObject(42);

Num := LongInt(StringList.Objects[Index]);

--
Ray Lischner                              li...@tempest-sw.com
Tempest Software, Corvallis, Oregon, USA  http://www.tempest-sw.com

Re:How to store a number in TStringList?


In article <31ac7c69.81531...@news.proaxis.com>, li...@tempest-sw.com says...

Quote

>On Mon, 27 May 1996 16:43:15 -0300, ak...@ccn.cs.dal.ca wrote:

>>TStringlist allows an object to be associated with an item. What I want
>>to do is just associating a number with the string item in the
>>Stringlist object. How to do it? Your help would be appreciated.

>Just type cast the number to TObject and vice versa:

>StringList.Objects[Index] := TObject(42);

>Num := LongInt(StringList.Objects[Index]);

>--
>Ray Lischner                              li...@tempest-sw.com
>Tempest Software, Corvallis, Oregon, USA  http://www.tempest-sw.com

GREAT IDEA RAY!

Re:How to store a number in TStringList?


Quote
 ak...@ccn.cs.dal.ca wrote:
>TStringlist allows an object to be associated with an item. What I want
>to do is just associating a number with the string item in the
>Stringlist object. How to do it? Your help would be appreciated.

if the TStringList variable is named v, then do this:

For i := 0 to v.Count - 1 do
   v.Objects[i] := TObject(1);

This would store a 1 in every row.  Then, to get the number out:

i := longint(v.Objects[i]);

Re:How to store a number in TStringList?


Quote
Robert Small (r...@melbpc.org.au) wrote:

: li...@tempest-sw.com (Ray Lischner) wrote:

: >>TStringlist allows an object to be associated with an item. What I want
: >>to do is just associating a number with the string item in the
: >>Stringlist object. How to do it? Your help would be appreciated.
: >
: >Just type cast the number to TObject and vice versa:
: >
: >StringList.Objects[Index] := TObject(42);
: >
: >Num := LongInt(StringList.Objects[Index]);
: >

: Tried this, and it works fine.

: I need to store the numbers (and strings) in a file when the app
: exits, and read them in the next time the app starts.

: But if I write it out to a file,

:       StringList.SaveToFile('myfile');

: and then later read back in from the file,

:       StringList.LoadFromFile('myfile');

: then Num is always 0.
: ==
: Bob Small

Bob,

Only the strings are saved to the file.  It was probably considered a
useless feature to save pointers to objects in a file, when they couldn't
be read from a file and used on subsequent runs.

Another possibility is to use the 'Values' property of the TStringlist
which will allow you to store strings in the format:

name=value

much like an INI file. You could put the string in the 'name' portion and
the number converted to a string in the 'Value' portion. This way you can
save the strings and numbers together with the 'SaveToFile' method.

Check out the 'Values' property of TStringlist in the Help system for the
details on how to do this.

Hope this helps,

-Ray
--
Ray Hildreth  
Greater Columbus Free-Net
rh...@freenet.columbus.oh.us

Re:How to store a number in TStringList?


Quote
li...@tempest-sw.com (Ray Lischner) wrote:
>>TStringlist allows an object to be associated with an item. What I want
>>to do is just associating a number with the string item in the
>>Stringlist object. How to do it? Your help would be appreciated.

>Just type cast the number to TObject and vice versa:

>StringList.Objects[Index] := TObject(42);

>Num := LongInt(StringList.Objects[Index]);

Tried this, and it works fine.

I need to store the numbers (and strings) in a file when the app
exits, and read them in the next time the app starts.

But if I write it out to a file,

        StringList.SaveToFile('myfile');

and then later read back in from the file,

        StringList.LoadFromFile('myfile');

then Num is always 0.
==
Bob Small

Other Threads