Page 2 of 2 <12
Topic Options
#138198 - 2005-04-20 06:44 PM Re: General Question about Using Array Data and how to use their indicies
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Richard -- I guess I've never had a problem using vars in strings before which is why I don't see why everyone is so against it.




Aye, and there's the rub.

It's fine while it works - when it doesn't work its a real problem and it may take you a while to discover why your previously well behaved program has stopped working.

You don't need to set NoVarsInStrings. Just don't use vars in strings.

It's just bad coding practice. It doesn't mean your code won't work just as well as code which doesn't have vars in strings. However you are more likely to encounter a situation where it will fail, and even worse is that you are less likely to know about it.

You're probably fed up with examples by now, but here is a good example of code not working as you'd expect:
Code:
If "$VAR" = "" "Variable $$VAR is not set " ? Else "Variable $$VAR is set" ? EndIf
If $VAR = "" "Variable $$VAR is not set " ? Else "Variable $$VAR is set" ? EndIf



The two conditionals are identical, except one has the variable in a string.

The output from this is:
Code:
Variable $VAR is set
Variable $VAR is not set



Now, that first result can't possibly be what the coder intended, can it?

Top
#138199 - 2005-04-20 07:02 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
No I don't think it would be their intention but my latest question still hasn't been answered: You don't have to use vars between quotes even when NoVarsInStrings is off do you? i.e.

even when I use vars in strings, if I'm testing to see if a var is set or not, I'd use your second example over the first. The only time I actually use vars in strings is when I'm 'echoing' a string and have a var in it. I mean, in your first example (if you can even do it), why would you use quotes when you don't need it?

Top
#138200 - 2005-04-20 07:48 PM Re: General Question about Using Array Data and how to use their indicies
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

You don't have to use vars between quotes even when NoVarsInStrings is off do you?



No you don't have to.

Whether NoVarsInStrings is on or off, don't put vars in strings. You do need to be aware of the setting of NoVarsInStrings if you are using the "$" character in a string as a real character rather than as a variable metacharacter.

Quote:

I mean, in your first example (if you can even do it), why would you use quotes when you don't need it?




I did do it. That's real code and real results cut'n'pasted from my own console.

The example illustrates the mechanism by which even simple code can get screwed up, I'm sure you can extrapolate the example to some more realistic scenarios.

Top
#138201 - 2005-04-20 08:01 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
I do understand what you're saying but in all examples, I'd be able to escape out of the quotes to get the result I'm looking for. I guess it's just going to take what you said Richard. Thanx for trying to steer me clear of going headlong off a cliff but I usually end up doing things the hard way first.

=P

Top
#138202 - 2005-04-24 10:01 PM Re: General Question about Using Array Data and how to use their indicies
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Then maybe this will help. the option to have vars inside strings beign procesed can be deprecated int he future. I remember Ruud making such a statement some time ago. Also, by using the NoVarsInStrings=ON option one does nto have to worry about the regular '$' sign charatcer in strings. With NoVarsInStrings=ON a single $-sign is sufficient, which comes in very handy if you start using the EXECUTE() function or KiXforms.

So, take some advise for the old guys, and try to code cleaner, life will be easier in the long run.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 2 of 2 <12


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.056 seconds in which 0.026 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org