Solved

Passing TStrings without creating them.

Posted on 2001-08-20
18
451 Views
Last Modified: 2010-04-06
Hi.

This is, I know, a silly question, but I'm sure I've seen it done.

If you have this procedure, (the procedure itself is irrelevant to this question).....

  procedure DoStuff(sValues: TStrings);

.....rather than create a TString and add the lines so that you can send it to the procedure, I'm sure that I've seen something - not this - but like this....

  DoStuff(['value1', 'value2', 'value3']);

(I know that that's a set, but it's something similar to that.)

Does anyone know what I mean?

John.
0
Comment
Question by:Jaymol
  • 6
  • 6
  • 3
  • +2
18 Comments
 
LVL 3

Expert Comment

by:Slavak
ID: 6406514
sounds strange, are u sure that parameter of TStrings type?
0
 
LVL 6

Author Comment

by:Jaymol
ID: 6406525
Yes.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6406534
use this declaration

procedure DoStuff(strings : array of String);

then you can use

DoStuff(['value1', 'value2', 'value3']);

for iterating use

for i := low(strings) to high(strings) do ...

meikl ;-)
0
 
LVL 6

Author Comment

by:Jaymol
ID: 6406540
Thanks, but that's not what I want.  I could do that no problem but I want to know how to pass a TString in that manner.

John.
0
 
LVL 3

Expert Comment

by:Slavak
ID: 6406553
Don't think it possible :)
0
 
LVL 6

Author Comment

by:Jaymol
ID: 6406597
Sorry to disagree, but myself and a colleague are sure we've seen this.

John.
0
 
LVL 3

Expert Comment

by:Slavak
ID: 6406646
so, I will like to see it too
0
 
LVL 6

Expert Comment

by:reddarin
ID: 6406798
Was it perhaps an overloaded function?

procedure DoThings(value: TStrings);overload;
procedure DoThings(value: array of strings);overload;

DoThings(value: array of strings);
var
  iList: TStrings;
begin
//
end;
reddarin
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6406804
no, you may misseen this,
or you see a method for adding in an already created Tstring-Object
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 6

Author Comment

by:Jaymol
ID: 6406808
No.  I've seen what I've asked how to do - honestly!

John.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 6406857
not with the tstrings-object,

but you may have seen a function which returns a tstringlist like

Function GetStringList(Strings : array of string) : TStringList;
var i : Integer;
begin
  Result := TstringList.Create;
  for i := low(strings) to high(strings) do
    Result.add(strings[i]);
end;

now you could call

 DoStuff(GetStringList(['value1', 'value2', 'value3']));

keep in mind you must free the stringlist after

meikl ;-)
 
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6406872
good point,  reddarin ;-)
0
 
LVL 6

Author Comment

by:Jaymol
ID: 6406893
meikl : nice.
0
 
LVL 1

Expert Comment

by:martin_g
ID: 6408542
kretzschmar,
I'm curious or maybe simple ninded but how do you release the TStringlist in the

                    Function GetStringList(Strings : array of string) : TStringList;
                     var i : Integer;
                     begin
                      Result := TstringList.Create;
                      for i := low(strings) to high(strings) do
                        Result.add(strings[i]);
                     end;
?
                     
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6408645
martin_g,

i didn't free it, because it is used later,
therefore i have added this sentence

>keep in mind you must free the stringlist after

meikl ;-)
0
 
LVL 1

Expert Comment

by:martin_g
ID: 6411993
I guess I just need an example.  What I was thinking, probably in error : )  is that a stringlist created within a function had to be freed within that function.  
To free the function result  stringlist, would you do something like the following:

Procedure DoSomething;
var
myStringlist:TStringlist;

begin
MyStringList:=GetStringList(MyStrings);
>>MyStringList.Free;
end;

Does MyStringlist.Free free the GetStringList Tstringlist result?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6412120
>Does MyStringlist.Free free the GetStringList Tstringlist result?
yes
0
 
LVL 6

Author Comment

by:Jaymol
ID: 6421224
Thanks guys.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now