Solved

how to return a function a stringList?

Posted on 2006-07-07
9
346 Views
Last Modified: 2010-04-05
Hello Guys

I would like to make a function where it could return me a stringList, something like this I am trying to do


procedure TFRELSUGEST.SBT_IMPRClick(Sender: TObject);
Var
 TQ : Tadoquery;
begin
 TQ := Tadoquery.Create(nil);
 TQ.Connection := Mycon;

 Try
   With TQ do
   Begin
      Sql.Clear;
      SQl.AddStrings(MyStringList);
      Open;
   End
     Run another code here
 Finally
  FreeAndNil(TQ)
 End ;


Function MyStringList: TstringList;
Var
  Str: TStringList;
Begin
  Str := TstringList.Create;
  Str.Add('SELECT * FROM TABLE');
  Str.Add('WHERE CODE = 102');
  Result := Str
  Str.Free
End
 
end;
0
Comment
Question by:hidrau
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 28

Assisted Solution

by:ciuly
ciuly earned 100 total points
Comment Utility
don't do
Str.Freeas it will destroy your apllication. instead, after using the function and no longer needing the stringlist, then do the free.

anitehr workaround is to make it a procedure, create it once in your app and pass it as parameter, then do a clear and just add lines and done.
0
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 50 total points
Comment Utility
You can change it to string. If you know that the string will be less than 2GB

Function MyStringList: String;
Var
  Str: TStringList;
Begin
  Str := TstringList.Create;
  Str.Add('SELECT * FROM TABLE');
  Str.Add('WHERE CODE = 102');
  Result := Str.Text;
  Str.Free
End

Then you can use it as follows

      Sql.Clear;
      SQl.Text := MyStringList;
0
 
LVL 1

Author Comment

by:hidrau
Comment Utility
If I don't use Str.free, will it  not be destroyed automatically when my function ends?
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
Yes, but then you will have a memory leak here:

 Try
   With TQ do
   Begin
      Sql.Clear;
      SQl.AddStrings(MyStringList); <-- string list allocated and never freed
      Open;
   End
     Run another code here
 Finally
  FreeAndNil(TQ)
 End ;

As Ciuly said, it is the preferred style (safety wise) to write a procedure that takes a TStrings parameter which you then fill in. The function/procedure CALLER is then responsible for both allocating/freeing the object.

Regards,
Russell

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:hidrau
Comment Utility
to pass myStringlist as a parameter, how could it be? I am not so sure about
0
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
something like:

procedure MyStringList(str: TstringList);
Begin
  Str.Clear;
  Str.Add('SELECT * FROM TABLE');
  Str.Add('WHERE CODE = 102');
End;
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 300 total points
Comment Utility

Full example below (better to use TStrings param, as it can accept both TStrings and TStringList)

----

procedure MyStringList(Value: TStrings);
begin

  if Assigned(Value) then
  begin
     Value.BeginUpdate;
     try
        Value.Clear;
        Value.Add('SELECT * FROM TABLE');
        Value.Add('WHERE CODE = 102');
     finally
        Value.EndUpdate;
     end;
  end;

end;

procedure TFRELSUGEST.SBT_IMPRClick(Sender: TObject);
var  TQ:            TAdoquery;
begin

  TQ:=TAdoQuery.Create(nil);
  try
     TQ.Connection:=Mycon;
     MyStringList(TQ.SQL);
     TQ.Open;
  finally
     FreeAndNil(TQ);
  end;

end;
0
 
LVL 11

Expert Comment

by:calinutz
Comment Utility
does your PROCEDURE return something....??
Maybe you should try using a function instead...?


Regards
0
 
LVL 6

Assisted Solution

by:House_of_Dexter
House_of_Dexter earned 50 total points
Comment Utility
Rllibby version is the way you want to go...

Never return a Live Object from a function...Who's in charge of freeing it?  Just a Memory Leak waiting to happen...

Instead pass the Live Object as a parameter...add your functionality...in the procedure...

procedure MyProc(aStrings: TStrings);
begin
  if Assigned(aStrings) then
  //do stuff here...
end;


procedure CallingProc;
var
  a_StringList: TStrings;
begin
  a_StringList := TStringList.Create;
  try
    MyProc(a_StringList);
    ListBox1.Assign(a_StringList);
//or just do this MyProc(ListBox1.Items);
  //you can play with a_StringList here if you need to...
  finally
    a_StringList.Free;
  end;
end;


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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