Board index » delphi » Function returning TStringList

Function returning TStringList

How can I return a TStringList *without* having a variable
of that type. I had problems with it. I can't seems to return
a TStringList created dynamically. I don't want to use a
variable because it would be a waste of memory (The real
data and the data used as a return space).

Is it possible to do something line memo1.lines := thefunction;

Thanks for the help.

 -DeathStroke

 

Re:Function returning TStringList


Quote
DeathStroke wrote:
> How can I return a TStringList *without* having a variable
> of that type. I had problems with it.

What problems, precisely?

Quote
> I can't seems to return
> a TStringList created dynamically. I don't want to use a
> variable because it would be a waste of memory (The real
> data and the data used as a return space).

> Is it possible to do something line memo1.lines := thefunction;

Sure it is. Maybe your problem is that you don't ever Create the
stringlist. This example works:

function MakeCityList: TStringList;
begin
  Result := TStringList.Create;
  Result.Add('New York');
  Result.Add('London');
  Result.Add('Stockholm');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines := MakeCityList;
end;

Re:Function returning TStringList


Quote
> How can I return a TStringList *without* having a variable
> of that type. I had problems with it. I can't seems to return
> a TStringList created dynamically. I don't want to use a
> variable because it would be a waste of memory (The real
> data and the data used as a return space).

Good question!
CAN YOU HAVE A TOBJECT AS A RESULT FOR A FUNCTION?

I had this problem for a while...here's what I did...

Procedure addthestrings(var whatstrings:tstringlist);
begin
whatstrings.addstring ('Hello!');
whatstrings.addstring ('Hello2!');
end;

procedure button1.click(sender:tobject);
begin
addthestrings(memo1.lines);
end;

I doubt this chews much memory at all, since I'm currently using it
in a program that recursively calls itself over to 5000 times with lists
up to 200 words long.

Let me know if anyone has another approach.

--
--------------------------
Eric Lawrence
Delta Programming Group
Delta...@juno.com

Re:Function returning TStringList


Quote
> How can I return a TStringList *without* having a variable
> of that type. I had problems with it. I can't seems to return
> a TStringList created dynamically. I don't want to use a
> variable because it would be a waste of memory (The real
> data and the data used as a return space).

I was always under the impression that since references were "really"
pointers, if I did something like:

Function CreateList: TStringList;
  var
    NewList: TStringList;
  begin
  NewList := TStringList.Create;
  NewList.Add('Hello there!');
  Result := NewList;
  end;

I was not actually creating two StringLists - just one that I was passing
back "by reference".

Brian

Re:Function returning TStringList


Quote
Eric Matthew Lawrence wrote:
> > How can I return a TStringList *without* having a variable
> > of that type. I had problems with it. I can't seems to return
> > a TStringList created dynamically. I don't want to use a
> > variable because it would be a waste of memory (The real
> > data and the data used as a return space).
> Good question!
> CAN YOU HAVE A TOBJECT AS A RESULT FOR A FUNCTION?
> I had this problem for a while...here's what I did...
> Procedure addthestrings(var whatstrings:tstringlist);
> begin
> whatstrings.addstring ('Hello!');
> whatstrings.addstring ('Hello2!');
> end;
> procedure button1.click(sender:tobject);
> begin
> addthestrings(memo1.lines);
> end;
> I doubt this chews much memory at all, since I'm currently using it
> in a program that recursively calls itself over to 5000 times with lists
> up to 200 words long.
> Let me know if anyone has another approach.

what do you mean?

I have message classes derived from TObject, and this works fine :

    // the following functions get the packet that starts with block
type flag
    function AnalyzeIDPacket(Pack : packet) : TCLTnppIDMessageClass;
    function AnalyzeDataPacket(Pack : packet) : TCLTnppDataMessageClass;
    function AnalyzeCapPacket(Pack : packet) : TCLTnppCapMessageClass;
    function AnalyzeCommandPacket(Pack : packet) :
TCLTnppCommandMessageClass;
    function AnalyzeStatusPacket(Pack : packet) :
TCLTnppStatusMessageClass;
    function AnalyzeGenericPacket(Pack : packet) :
TCLTnppGenericMessageClass;

I don't see why you would not be able to return ie. stringlist

--
It's not a fault of Windows NT, it's a fault of Windows 95
----------------------------------------------------------
DSpa...@rotocast.com, d...@cyclelogic.com

Other Threads