Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual FoxPro 9 Variables 1

Status
Not open for further replies.

gbcpastor

Programmer
Jun 12, 2010
77
US
I'm working on someone else's code and I've run across something I've not seen before. They have variables with an ampersand in the middle of them example statecount&version

Not sure what to make of this, can someone please enlighten me? I have to fix this code and I need to know what this is.

Thanks in advance.
 
Do you know Foxpro macro substitution?

Most likely a variable [tt]version[/tt] exists at the time of execution and is set to "1","2", maybe also a suffix like "total" or "partial". No matter whats the string value of [tt]version[/tt] is at the time this is executed, it is substituted at its place, so the real name - whether variable or field name - accessed via [tt]statecount&version[/tt] then is (in order of my examples) [tt]statecount1, statecount2, statecounttotal[/tt] or [tt]statecountpartial[/tt]. The [tt]version[/tt] variable may be read from data, set via STR() converting a FOR loop counter or anything else, it just needs to be a string.

If you know macro substitution, you might rather only use it stand alone, eg prepare an SQL statement in [tt]lcSQL[/tt] and then have [tt]&lcSQL[/tt] to execute it or have a where clause and then [tt]SELECT * FROM somteable WHERE &lcWhere[/tt], then just notice macro substitution can not only be used standalone as usual or at the end of an expression as here, but also in the middle of anywhere, eg you can set [tt]mystring="een.Cap"[/tt] and when you then execute [tt]? _Scr&mystring.tion[/tt] this results in printing _screen.Caption on the screen, which also makes use of the most seldom known end of macro substitution dot to tell VFP the "tion" portion is not part of the variable name of the substituted variable, so [tt]mystring[/tt] is the variable substituted, not [tt]mystringtion[/tt]. If you declare an object _Screening with a property Assertion and set mysrtring to "eening.Asser" the same code would print out _screening.Assertion, but of course substitutions in the middle of something is less useful to dynamically toggle what is read/printed/executed.

In no way an ampersand can be part of a name, the only other use of it is double ampersand && for beginning a comment in a line with code up to that point instead of * as comment start of a whole comment line.

If you don't get it and don't have VFP at hand to test and debug it, post a bit of code around this line up to a point the version variable is declared and/or set and we could see whether we can "translate" this to mere mortal English.

Bye, Olaf.
 
The macro substitution is a cute trick but if you don't comment your code, it turns into a nightmare real quick.

Thank you so much for getting to this so quickly.
 
Well, it's quite a usual part of any FoxPro program to make use of it, like ++ is for C developers. I agree comments are always good, but I also don't expect explanation of sql specialties, just because they are not common in other databases. You're welcome here, with such questions specific to FoxPro, of course.

What's more of a nightmare is using it for anything. Foxpro is totally capable of declaring and using arrays, too, so the developer could have dimensioned a statecount[] array and set statecount[1], statecount[2] etc.

Bye, Olaf.
 
Wow, version is also a lousy name for a variable since it is a FoxPro function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top