if not Assigned (myStringList)
then myStringList := tStringList.Create;
if myStringList.Count < 1
then loadMyStringList;
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.
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.
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
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
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 --
That's not true. If the variable was already assigned, then it might
refer to an object with a non-zero count.
--
Rob
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 . . ."