BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Referencing a stringlist by name

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Mark Shapiro
Guest





PostPosted: Fri Jan 28, 2005 3:33 pm    Post subject: Referencing a stringlist by name Reply with quote



I would like to create a generic test routine that checks whether or
not any of a series of TStringLists contains any text. How can I
duplicate the following type of functionality for a TStringList? At
runtime, the name of the list will be contained in the CTable
"control_name" field.

if (FindComponent(CTable.FieldByName('control_namel').asString) as
TRichEdit).Text = '' then
Result := false;

Back to top
Tom de Neef
Guest





PostPosted: Fri Jan 28, 2005 5:53 pm    Post subject: Re: Referencing a stringlist by name Reply with quote



"Mark Shapiro" <infocus (AT) swbell (DOT) net> schreef in bericht
news:5dmkv05bpdi3u25co0p979uglqssh83r5l (AT) 4ax (DOT) com...
Quote:
I would like to create a generic test routine that checks whether or
not any of a series of TStringLists contains any text. How can I
duplicate the following type of functionality for a TStringList? At
runtime, the name of the list will be contained in the CTable
"control_name" field.

if (FindComponent(CTable.FieldByName('control_namel').asString) as
TRichEdit).Text = '' then
Result := false;


A Tstringlist has a 'text' property: this lists the strings in the object as
a single string with the individual strings delimited by carriage returns
and line feeds. So, checking for the presence of a refString within one
Tstringlist can be done via:
result:= NOT pos(refString,myStringlist.text);
or one of the more general string searching functions if you do not care
about uppercase/lowercase.

When you have a series of Tstringlists, the above can be extended to:
result:=true;
for k:=0 to series.count-1 do
if pos(refString,Tstringlist(series[k]).text)
then exit;
result:=false;

You will have to store the stringlists in Series:
var series : Tlist;
begin
series:=Tlist.create;
series.add(firstStringlist);
series.add(secondStringlist);
....
or a more appropriate way of identifying them and storing them in the Series
list.
Tom



Back to top
Mark Shapiro
Guest





PostPosted: Fri Jan 28, 2005 9:43 pm    Post subject: Re: Referencing a stringlist by name Reply with quote



Thank you Tom. What I'm looking for is a bit different: how to
reference a StringList programmatically when I won't know its name
until runtime.

if SomeStringList.Text = '' then
Result := false;

I have the name of the StringList variable as a string. How do I cast
it or otherwise reference it to achieve the functionality of the code
above?
Back to top
Tom de Neef
Guest





PostPosted: Fri Jan 28, 2005 10:24 pm    Post subject: Re: Referencing a stringlist by name Reply with quote

"Mark Shapiro" <infocus (AT) swbell (DOT) net> schreef in bericht
news:68clv0d3v5qcdl7ia46icssi55g024rifq (AT) 4ax (DOT) com...
Quote:
Thank you Tom. What I'm looking for is a bit different: how to
reference a StringList programmatically when I won't know its name
until runtime.

if SomeStringList.Text = '' then
Result := false;

I have the name of the StringList variable as a string. How do I cast
it or otherwise reference it to achieve the functionality of the code
above?

Somehow, its name should be stored somewhere.
Suppose you get it from a database, where the list of strings is returned as
the result of a query. In the query you specify the name, or the name is
returned as well.

var
listOfTables : Tstringlist;
aTable : Tstringlist;
tableName : string;
begin
listOfTables:=Tstringlist.create;
...
//get your table from somwhere
aTable:=Tstringlist.create;
fillTheTable(tableName,aTable); // returns name and list
// add name to ListOfTables
ListOfTables.add(tableName);
// associate the table with this name
ListOfTables.objects[ListOfTables.count-1]:=aTable;
...
// check if string is present - to be packed in a function
for k:=0 to ListOfTables.count-1 do
if pos(searchString,Tstringlist(ListOfTables.objexts[k]).text)>0
then found

To check if a searchString is present in the table with name TableName:
function checkPresent(searchString,targetTable : string; tables :
Tstringlist) : boolean;
var indx : integer;
begin
indx:=tables.IndexOf(targetTable);
result:=(indx>=0) AND Tstringlist(tables[indx]).IndexOf(searchString)>=0)
end;

You can also make the table name a property of a descendant of Tstringlist:
type
Ttable = class(Tstringlist)
tableName : string;
end;

In that case you can then store the Ttables in a Tlist.
Hope I am closer now.
Tom



Back to top
VBDis
Guest





PostPosted: Sun Jan 30, 2005 2:38 am    Post subject: Re: Referencing a stringlist by name Reply with quote

Im Artikel <68clv0d3v5qcdl7ia46icssi55g024rifq (AT) 4ax (DOT) com>, Mark Shapiro
<infocus (AT) swbell (DOT) net> schreibt:

Quote:
I have the name of the StringList variable as a string. How do I cast
it or otherwise reference it to achieve the functionality of the code
above?

Put your stringlists in another stringlist, together with their names. Then
search that stringlist for the stringlist name, obtain the according stringlist
object, and search it for the string as already outlined.

DoDi

Back to top
AlanGLLoyd
Guest





PostPosted: Sun Jan 30, 2005 9:26 am    Post subject: Re: Referencing a stringlist by name Reply with quote

In article <5dmkv05bpdi3u25co0p979uglqssh83r5l (AT) 4ax (DOT) com>, Mark Shapiro
<infocus (AT) swbell (DOT) net> writes:

Quote:
I would like to create a generic test routine that checks whether or
not any of a series of TStringLists contains any text. How can I
duplicate the following type of functionality for a TStringList?

The name of a control or other object is a design-time convenience. It is not
available at run-time unless (again for convenience) the object has a field
called Name in which the design-time string is stored.

Use the reference to the object. Or if you _must_ have design-time names
accessible at run-time make a descendant of TStringList which has a Name
property in which you place the name in your code when it is created.

Or do as DoDi recommends ...

LookUpSL.AddObject('MySLName', MySLName); // add for every stringlist

FindSL := TStringList(LookUpSL.Objects[LookUpSL.IndexOf('MySLName')]);

if FindSL.Text = '' then
Result := false;

Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]

Back to top
Bruce Roberts
Guest





PostPosted: Mon Jan 31, 2005 7:27 pm    Post subject: Re: Referencing a stringlist by name Reply with quote


"Mark Shapiro" <infocus (AT) swbell (DOT) net> wrote

Quote:
I would like to create a generic test routine that checks whether or
not any of a series of TStringLists contains any text. How can I
duplicate the following type of functionality for a TStringList? At
runtime, the name of the list will be contained in the CTable
"control_name" field.

if (FindComponent(CTable.FieldByName('control_namel').asString) as
TRichEdit).Text = '' then
Result := false;

function EmptyStrings (sl : tStrings) : boolean;

begin
result := sl = '';
end;

Is a generic tStrings test. If you really need a data driven tStrings check
you might want to take a look at the routines in TypInfo. Depending on where
your lists are declared, you may be able to use something there.

OT. I have to say that wanting to do this smells to me of poor design
choice. While there are many exceptions, I've generally found that writing
truly generic application code an exceedingly expensive, total waste of
effort.

If you need string lists that have to alter their behavior when they are
empty, write and use a tStringList descendant instead.




Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.