• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

Passing TStrings without creating them.

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
Jaymol
Asked:
Jaymol
  • 6
  • 6
  • 3
  • +2
1 Solution
 
SlavakCommented:
sounds strange, are u sure that parameter of TStrings type?
0
 
JaymolAuthor Commented:
Yes.
0
 
kretzschmarCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
JaymolAuthor Commented:
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
 
SlavakCommented:
Don't think it possible :)
0
 
JaymolAuthor Commented:
Sorry to disagree, but myself and a colleague are sure we've seen this.

John.
0
 
SlavakCommented:
so, I will like to see it too
0
 
reddarinCommented:
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
 
kretzschmarCommented:
no, you may misseen this,
or you see a method for adding in an already created Tstring-Object
0
 
JaymolAuthor Commented:
No.  I've seen what I've asked how to do - honestly!

John.
0
 
kretzschmarCommented:
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
 
kretzschmarCommented:
good point,  reddarin ;-)
0
 
JaymolAuthor Commented:
meikl : nice.
0
 
martin_gCommented:
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
 
kretzschmarCommented:
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
 
martin_gCommented:
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
 
kretzschmarCommented:
>Does MyStringlist.Free free the GetStringList Tstringlist result?
yes
0
 
JaymolAuthor Commented:
Thanks guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 6
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now