?
Solved

how to return a function a stringList?

Posted on 2006-07-07
9
Medium Priority
?
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 28

Assisted Solution

by:2266180
2266180 earned 400 total points
ID: 17058419
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 200 total points
ID: 17058500
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
ID: 17058507
If I don't use Str.free, will it  not be destroyed automatically when my function ends?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Russell Libby
ID: 17058642
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
 
LVL 1

Author Comment

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

Expert Comment

by:2266180
ID: 17058726
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 1200 total points
ID: 17058910

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
ID: 17059122
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 200 total points
ID: 17059869
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

801 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