Rick Strahl's Weblog
Rick Strahl's FoxPro and Web Connection Weblog
White Papers | Products | Message Board | News |

Unwanted Macro Expansion in FoxPro Class String Properties

1 comment
December 31, 2010 •

Ran into a weird issue today from somebody using our Web Service Proxy Generator tool for Visual FoxPro. The Web Service generator will create a FoxPro client class that calls a Web Service and so generates a class with properties and methods that match the Web Service signature plus a few operational properties like the URL.


As part of the class a property might be generated like this:


DEFINE CLASS WebStoreConsumerServiceProxy as Custom


cServiceUrl = "http://www.west-wind.com/wwstore/Service/WebStoreConsumerService.asmx?parm1=x&parm2=y"




Notice that the URL includes a &parm=y at the end.


If you try to instantiate this class it will fail. In fact compiling this code will result in a compiler error:


cServiceUrl = [http://www.west-wind.com/wwstore/Service/WebStoreConsumerService.asmx?parm1=x&parm2=y]

Error in line 10: Statement is not valid in a class definition.


The reason this happens is because the &parm is being parsed as a Macro expression by the VFP compiler, which in this case is obviously incorrect. But oddly this particular bug only shows up in class definitions not in code. If you do something like this:


o = CREATEOBJECT("WebStoreConsumerServiceProxy")

o.cServiceUrl = [http://www.west-wind.com/wwstore/Service/WebStoreConsumerService.asmx?parm1=x&parm2=y]

? o.cServiceUrl


The code works fine.


So the issue here is that it only occurs in a class definition, not in strings in regular code, which is clearly a bug in the VFP code parser and it’s one to watch out for.


For my tool this is a pretty big issue however, because & in URLs are actually quite common and since a generator generates this code it’s tricky to catch. One way to get around this is to ‘escape’ the string with the embedded macro expression inside of the class generator:


o.cServiceUrl = [http://www.west-wind.com/wwstore/Service/] +;
                [WebStoreConsumerService.asmx?parm1=x] + "&" + [parm2=y]


Explicitly breaking out the & and adding it as a character fixes this problem.


As a side note – and probably the reason this problem doesn’t show up much more commonly – is that if there’s a space after the & this problem doesn’t occur.


So the following works just fine:


cServiceUrl = "Simon & Schuster"


in a class definition. Go figure… Just another VFP oddity to remember and be aware of.

Posted in:

Feedback for this Weblog Entry


Roger Zacharczyk
March 21, 2011

Hi there,

Sorry for posting here - couldn't find your e-mail address.

I'm the leading editor of Software Developer's Journal which is a new e-magazine. I'm looking for developers who want to share their knowledge with other developers. If you are interested in cooperation (articles writing, proofreading, ads) just please let me know - roger.zacharczyk@software.com.pl

© Rick Strahl, West Wind Technologies, 2003 - 2023