Solved

how to return a function a stringList?

Posted on 2006-07-07
9
362 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:2266180
2266180 earned 100 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 50 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 300 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 50 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 92
Firemonkey allowing RTL on android 6 47
Delphi: sending SMS on android platform 1 39
How to make Sign in, using Clientdataset? 1 28
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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