Solved

how to return a function a stringList?

Posted on 2006-07-07
9
352 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
creating manifest for my dll that called from activex 6 99
Delphi cmd execution 6 56
Dev Express grid collapse 2 39
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 57
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

18 Experts available now in Live!

Get 1:1 Help Now