Board index » delphi » TStringList can save strings, but integers?

TStringList can save strings, but integers?

Hi Guys,

I know you can save strings dynamically by simply doing the following:

Var X : TStringList;
begin
  x:=TStringList.Create;
  x.Add('A');
  x.Add('B');
  x.Add('C');
  x.free;
end;

But how can I save Integer-Values in such a data-array not by doing
x.Add(inttostr(12345)); ?

        Bye

                Joe

======================================================
johannes_ba...@gmx.TUDASWEG.de
To mail me, delete the underscore (_), and remove all Capslock-Letters.
======================================================

 

Re:TStringList can save strings, but integers?


You should still be able to use a TStringList to store an integer value
withut converting it. Look up the AddObject method. (Or am I thinking about
a TList object?). Anyway, the object can be any object, a tstring, aTQuery,
etc.
Quote
Joe wrote:
> Hi Guys,

> I know you can save strings dynamically by simply doing the following:

> Var X : TStringList;
> begin
>   x:=TStringList.Create;
>   x.Add('A');
>   x.Add('B');
>   x.Add('C');
>   x.free;
> end;

> But how can I save Integer-Values in such a data-array not by doing
> x.Add(inttostr(12345)); ?

>         Bye

>                 Joe

> ======================================================
> johannes_ba...@gmx.TUDASWEG.de
> To mail me, delete the underscore (_), and remove all Capslock-Letters.
> ======================================================

Re:TStringList can save strings, but integers?


Quote
Simon Reye wrote in message <369E8D38.39DC3...@aecorp.com.au>...
>If only Delphi had dynamic arrays like C/C++ huh.
>Does Delphi 4 have dynamic arrays (a rumour I heard)?

D4 does, and, yes, and they are very nice.

--
Grace + Peace  |  Peter N Roth  |  Engineering Objects Int'l
Amazon.com associate | Inprise Tool & Component Builder
          http://www.inconresearch.com/eoi

Re:TStringList can save strings, but integers?


Joe,

You could use a TList and muck around with pointers to integers (TList
stores pointers) which involves a bit of typecasting and all.

If only Delphi had dynamic arrays like C/C++ huh.

Does Delphi 4 have dynamic arrays (a rumour I heard)?

Simon

Quote
Joe wrote:
> Hi Guys,

> I know you can save strings dynamically by simply doing the following:

> Var X : TStringList;
> begin
>   x:=TStringList.Create;
>   x.Add('A');
>   x.Add('B');
>   x.Add('C');
>   x.free;
> end;

> But how can I save Integer-Values in such a data-array not by doing
> x.Add(inttostr(12345)); ?

>         Bye

>                 Joe

> ======================================================
> johannes_ba...@gmx.TUDASWEG.de
> To mail me, delete the underscore (_), and remove all Capslock-Letters.
> ======================================================

Re:TStringList can save strings, but integers?


In article <369e5642.934...@news.odn.de>, johannes_ba...@gmx.TUDASWEG.de (Joe)
writes:

Quote
>Var X : TStringList;
>begin
>  x:=TStringList.Create;
>  x.Add('A');
>  x.Add('B');
>  x.Add('C');
>  x.free;
>end;

>But how can I save Integer-Values in such a data-array not by doing
>x.Add(inttostr(12345)); ?

x.AddObject('A', pointer(12345));  // type-cast as Delphi expects a pointer
or x.InsertObject(0, 'A', pointer(12345);

 . .  and get them with

MyInt := integer(x.Objects[0]);  // type-cast back again

Having already stored a string you could then :-

x.Objects[1] := pointer(12345); // but the string _must_ have been added first.

A pointer is a DWord and so is an integer, so object storage can hold integers,
and also integer storage (Tag) can hold pointers (but you have to type-cast
each way).

Alan Lloyd
alangll...@aol.com

Re:TStringList can save strings, but integers?


Im Artikel <369E8D38.39DC3...@aecorp.com.au>, Simon Reye <s.r...@aecorp.com.au>
schreibt:

Quote
>Does Delphi 4 have dynamic arrays (a rumour I heard)?

Yes, it does :-)

But I don't know the cost, in bytes, so a TList will be a good solution anyhow.

DoDi

Re:TStringList can save strings, but integers?


Im Artikel <369E8F6A.D0A27...@wt.net>, Owen Hebert <ow...@wt.net> schreibt:

Quote
>Anyway, the object can be any object, a tstring, aTQuery,
>etc.

Perhaps you can help me with a similar problem. I try to store records in the
Objects array, or in a dynamic array. But I have problems with the compiler,
that rejects e.g. "with dynamic_array[i] do", at least if the array contains
references/pointers to records. How should references to records be put into a
dynamic or Objects array?

Example:
  type TMyRec = record ...
  x: array of TMyRec; // or array of pointer to TMyRec???
Will that array contain the records themselves, or only references to records,
that must be allocated(?) somewhere else?

DoDi

Re:TStringList can save strings, but integers?


In article <19990117011520.17758.00004...@ngol08.aol.com>, vb...@aol.com

Quote
(VBDis) writes:
>Example:
>  type TMyRec = record ...
>  x: array of TMyRec; // or array of pointer to TMyRec???
>Will that array contain the records themselves, or only references to
>records,
>that must be allocated(?) somewhere else?

Only references (pointers) to the records. You would have to write code to
store them to file.

Alan Lloyd
alangll...@aol.com

Re:TStringList can save strings, but integers?


Quote
VBDis wrote:

> Perhaps you can help me with a similar problem. I try to store records in the
> Objects array, or in a dynamic array. But I have problems with the compiler,
> that rejects e.g. "with dynamic_array[i] do", at least if the array contains
> references/pointers to records. How should references to records be put into a
> dynamic or Objects array?

> Example:
>   type TMyRec = record ...
>   x: array of TMyRec; // or array of pointer to TMyRec???
> Will that array contain the records themselves, or only references to records,
> that must be allocated(?) somewhere else?

Hmmh, I hope the following (untested) code has something to do with the
problem you describe. It uses TList to keep hold of a bunch of dynamic
records.

type
  DelaPtr=^DelaRec;
  DelaRec=Record
    OrderNo,CustNo,AmountTotal: Extended;
    DelayDays:Integer;
   end;

var
  DList:TList;
  Dpt :DelaPtr;
begin
  DList:=TList.Create; {Create TList}
  while not MyQuery.Eof do
  begin
    New(Dpt);          {Create a new dynamic record and}
    DList.Add(Dpt);    {add the record to DList.}
    Dpt^.OrderNo:=MyueryOrderNo.Value;
    Dpt^.AmountTotal:=MyQueryAmountTotal.Value;
    Dpt^.DelayDays:=MyQueryDelayDays.Value;
    MtQuery.Next;
  end;

  ...
 {After use, free all the created objects and records}
 for i:=0 to DList.Count-1 do
 Dispose(DList[i]); {Free all the dynamic records}
 DList.Free;        {Release the TList}
end;

You can also pass those dynamic records as a function parameter:

function TOrderForm.MakeInvoice(Var List:TList)
var
  n,i,j :Integer;
  DPt :DelaPtr;
  A,B,C:Extended;
begin
  n:=0;
  while n <= List.Count-1 do
  begin
    DPt:=List.Items[n];
    A:=Dpt^.OrderNo;
    B:=Dpt^.AmountTotal;
    j:=Dpt^.DelayDays;
    ...{do something with those values}
    ...
    inc(n);
  end;
end;

Markku Nevalainen

Re:TStringList can save strings, but integers?


Quote
VBDis wrote:

> Im Artikel <369E8F6A.D0A27...@wt.net>, Owen Hebert <ow...@wt.net> schreibt:

> >Anyway, the object can be any object, a tstring, aTQuery,
> >etc.

> Perhaps you can help me with a similar problem. I try to store records in the
> Objects array, or in a dynamic array. But I have problems with the compiler,
> that rejects e.g. "with dynamic_array[i] do", at least if the array contains
> references/pointers to records. How should references to records be put into a
> dynamic or Objects array?

> Example:
>   type TMyRec = record ...
>   x: array of TMyRec; // or array of pointer to TMyRec???
> Will that array contain the records themselves, or only references to records,
> that must be allocated(?) somewhere else?

When you declare "TMyRec" is a record, then it is a physical block of
storage (not a class, which is a pointer).  So "x" consists of physical
chunks.  The dynamic-array implementation "appears to be just like a
Pascal array" although its implementation is obviously different
under-the-covers.

But let me generalize a little to the question brought-up by the title
of this message.  A "TStringList" is just a container-class; one of
hundreds that you can buy or build or have-for-free.  (For that matter,
dynamic-arrays are just a more-built-in container class that is easier
to use, for some people, but implemented in the same way.

Any container-class implements "read" and/or "write" methods, possibly
"array-style," which are invoked (magically) when you use the
corresponding property in an assignment or other statement.  Array-style
methods allow you to make references in the form of an array-type
syntax.  But what ultimately happens is that a method-call takes place.
What you do within that method is not only (a) up to you, but (b)
invisible to all of the client-programs which use your class.
Therefore, (c) the implementation can be replaced or optimized without
breaking anything.

The "Component Writer's Guide" is a very thin book that is easy to
overlook -- but there is an enormous(!) amount of information in there.
The good thing is also that it's a well-written book and easy to read
(... slowly).

/mr/

Re:TStringList can save strings, but integers?


You can store integers in a Tlist or a TStringlist by casting them as
pointers

mystringlist.addobject('string',pointer(integer value));

or in a tlist

mylist.add(pointer(integer value));

remember to cast them back when reading them

integer(mylist.items[index])

John

Re:TStringList can save strings, but integers?


Im Artikel <36A219C3.3...@sundialservices.com>, Sundial Services
<i...@sundialservices.com> schreibt:

Quote
>The dynamic-array implementation "appears to be just like a
>Pascal array" although its implementation is obviously different
>under-the-covers.

I didn't dig deeper into the implementation, but found the answer to the most
interesting question: a dynamic array really allocates the memory for every
member.

Remaining questions about dynamic arrays:
- do assignments to members result in a physical copy?
- do arrays of some class contain references to the instances?
(I think that the answer is yes in both cases)

Or, in a wider scope:
- What's the difference between passing along classes and e.g. records? IMO
objects (of some class) are always handled by references, whereas records and
other data types are normally passed along as copies, unless for 'var'
parameters?

- What's the effect of 'var' on subroutine arguments of some class? Another
level of indirection?

- Are 'const' arguments also 'var', i.e. pointers?

Thank you all for your assistance :-)
  DoDi

Re:TStringList can save strings, but integers?


Quote
>- do assignments to members result in a physical copy?

Yep, they do, unless you assign the whole array.  This is because
internally, dynamic arrays are considered as ^array (as you may have seen in
the CodeInsight windows).  Classes derived from TPersistent, when you assign
them to another, e.g. MyObject1 := MyObject2 will make MyObject1 refer to
MyObject2's data (i.e., it just assigns the pointer MyObject2 has to
MyObject1).  However, if you do MyObject1.Assign(MyObject2), it will copy
the data.

Quote
>- do arrays of some class contain references to the instances?

Yes, the array is effectively as array of pointers to the actual object
data.

Quote
>Or, in a wider scope:
>- What's the difference between passing along classes and e.g. records? IMO
>objects (of some class) are always handled by references, whereas records
and
>other data types are normally passed along as copies, unless for 'var'
>parameters?

You're right.  Records have no referencing or pointers to do with them at
all, they just contain the data.  Thus, when you pass a record to a
procedure, the whole record will get put on the stack (I think).  If it's a
var paremeter, though, it will pass the address this data is stored at,
effectively passing @TheRecord.  When you pass an object, the pointer is
always passed.

Quote
>- What's the effect of 'var' on subroutine arguments of some class? Another
>level of indirection?

No, the only difference var will have on passing an object is to make sure
you are passing a variable.  It's passed as a pointer to the object no
matter what.

Quote
>- Are 'const' arguments also 'var', i.e. pointers?

Depends on whether what you passed was a simple type or record, or an
object.  If it's just a simple type or a record, it's passed by value (just
as it would if the const wasn't there).  However, if it's an object, the
pointer to the object is actually passed.  The const in this case protects
you from assigning a different pointer to that variable.  You can still
alter the properties of the object within the procedure.  When you have a
const argument, you cannot then pass this to another procedure by reference,
because the procedure presumably does this so it can alter its value.

Quote
>Thank you all for your assistance :-)
>  DoDi

That's fine.

Cameron
_______________________
cameronm...@hotmail.com

Other Threads