Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

StringList

1 view
Skip to first unread message

Stark

unread,
Sep 9, 2008, 2:46:47 PM9/9/08
to
I use a stringList to maintain values that I show in a page of a controlPage
when the specific tab is chosen. In order to avoid the re-creation of the
stringList each time the page is accessed I thought I would test if the
Stringlist was nil or empty, but I could'nt find the way. Can anyone suggest
how to do this ? Or perhaps a different way of doing this thing ? Thanks in
advance


BRoberts

unread,
Sep 9, 2008, 6:47:50 PM9/9/08
to
"Stark" <franco...@tin.it> wrote in message
news:48c6c542$0$40307$4faf...@reader5.news.tin.it...

if not Assigned (myStringList)
then myStringList := tStringList.Create;
if myStringList.Count < 1
then loadMyStringList;

Jamie

unread,
Sep 9, 2008, 7:17:16 PM9/9/08
to
Stark wrote:

At the start of your program in the formCreate event for example.
create all the stringlists but just don't add anything to them.

Use the "Count" property to test if the string contains items or
not.
When you're done, simply use the "Clear" method of the strings to
empty it.
This will keep the string object alive so you don't need to
worry about that.


http://webpages.charter.net/jamie_5"

Jamie

unread,
Sep 9, 2008, 8:16:47 PM9/9/08
to
BRoberts wrote:

That is not safe!.
Maybe in .NET it could be but otherwise no.

You have to insure that at least once, the object has be set to
NIL at start up. Then the "Assigned" function works as documented.
and if you use FreeAndNill or Nil after a Free so the next time
you check it, it should be NIL!.

Simply put, unless you initially set the object to NIL at start up
some where, you really can't rely on that to work unless you of course
know at some point it has never yet been created? then you simply
skip that approach the first time around.

I also do a NIL set of all objects at the start of the object that
maybe questionable in their validity at some point.

http://webpages.charter.net/jamie_5"

Rob Kennedy

unread,
Sep 9, 2008, 8:19:27 PM9/9/08
to
Stark wrote:
> I use a stringList to maintain values that I show in a page of a controlPage
> when the specific tab is chosen. In order to avoid the re-creation of the
> stringList each time the page is accessed I thought I would test if the
> Stringlist was nil or empty, but I could'nt find the way.

To check whether something is nil, check whether it's nil:

if foo = nil then ...

The check whether a list is empty, check its Count property:

if foo.Count = 0 then ...

--
Rob

Rob Kennedy

unread,
Sep 9, 2008, 8:23:36 PM9/9/08
to
Jamie wrote:
> BRoberts wrote:
>> "Stark" <franco...@tin.it> wrote in message
>> news:48c6c542$0$40307$4faf...@reader5.news.tin.it...
>>> I use a stringList to maintain values that I show in a page of a
>>> controlPage when the specific tab is chosen. In order to avoid the
>>> re-creation of the stringList each time the page is accessed I
>>> thought I would test if the Stringlist was nil or empty, but I
>>> could'nt find the way. Can anyone suggest how to do this ? Or perhaps
>>> a different way of doing this thing ? Thanks in advance
>>
>> if not Assigned (myStringList)
>> then myStringList := tStringList.Create;
>> if myStringList.Count < 1
>> then loadMyStringList;
>
> That is not safe!.
> Maybe in .NET it could be but otherwise no.

It's perfectly safe.

> You have to insure that at least once, the object has be set to
> NIL at start up.

Anything that has a scope large enough for the code above to not be
nonsense is already initialized to nil at startup.

> Then the "Assigned" function works as documented.
> and if you use FreeAndNill or Nil after a Free so the next time
> you check it, it should be NIL!.
>
> Simply put, unless you initially set the object to NIL at start up
> some where, you really can't rely on that to work unless you of course
> know at some point it has never yet been created? then you simply
> skip that approach the first time around.

Anything that has unit-level scope or class-level scope will be initialized.

Local variables aren't initialized, but if the variable in the code
above is a local variable, then the code is nonsense anyway. The
variable obviously needs a big enough scope that it will be available
while two different pages are visible. Local variables can't do that.

--
Rob

Henry B

unread,
Sep 17, 2008, 8:54:29 PM9/17/08
to

"BRoberts" <berdon...@caneris.ca> wrote in message
news:a8998$48c6fbc9$45c49fa8$25...@TEKSAVVY.COM-Free...

> "Stark" <franco...@tin.it> wrote in message
> if not Assigned (myStringList)
> then myStringList := tStringList.Create;
> if myStringList.Count < 1

At this point myStringList.Count must = 0 so what'e the poiny of testing it
it's < 1

> then loadMyStringList;


-- Posted on news://freenews.netfront.net - Complaints to ne...@netfront.net --

Rob Kennedy

unread,
Sep 18, 2008, 12:19:12 AM9/18/08
to
Henry B wrote:
> "BRoberts" <berdon...@caneris.ca> wrote in message
> news:a8998$48c6fbc9$45c49fa8$25...@TEKSAVVY.COM-Free...
>> if not Assigned (myStringList)
>> then myStringList := tStringList.Create;
>> if myStringList.Count < 1
>
> At this point myStringList.Count must = 0 so what'e the poiny of testing it
> it's < 1

That's not true. If the variable was already assigned, then it might
refer to an object with a non-zero count.

--
Rob

BRoberts

unread,
Sep 18, 2008, 5:41:04 PM9/18/08
to
"Henry B" <ham...@spamlock.microtech.com.au> wrote in message
news:gas8s4$6f2$1...@adenine.netfront.net...

>
> "BRoberts" <berdon...@caneris.ca> wrote in message
> news:a8998$48c6fbc9$45c49fa8$25...@TEKSAVVY.COM-Free...
>> "Stark" <franco...@tin.it> wrote in message
>> if not Assigned (myStringList)
>> then myStringList := tStringList.Create;
>> if myStringList.Count < 1
>
> At this point myStringList.Count must = 0 so what'e the poiny of testing
> it
> it's < 1
>

As Rob said, re the logic. I could have used "else if`" but the couple of
saved cycles don't in my opinion outweigh the added robustness of unnested
logic.

The <1 test is a very long established coding practice of mine. I always
avoid absolute tests in favor of relative ones. In the above case I doubt
that it makes a difference. But the practice is quite ingrained, and
purposely so. It helps me avoid writing turkey, bug potential code like "if
someFloatVariable = 0 then . . ."

0 new messages