Solved

how to return a function a stringList?

Posted on 2006-07-07
9
369 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 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
Technology Partners: 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 97
Tembedded WB animatid gifs not animated on some pcs 2 92
FMX and jaudiotracker playing memory stream 29 107
TlistView is Really heavy on Android 3 43
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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