Macro Substitution in VB.Net

Posted on 2005-04-06
Last Modified: 2008-01-09
In FoxPro you can use the ampersand character to indicate macro substitution. For example:

TableName = "Companies"
FieldName = "Location"
Use &TableName index &FieldName

FoxPro would see the '&' and substitute "Companies" in place of &TableName and "Location" in place of &FieldName.

Is there a way to do something similar in VB.NET?

Question by:Thanbor

    Expert Comment

    Unfortunately, the short answer is no (afaik).
    Sorry to be the bearer of bad news   :-P

    VB.NET does support the #Const and #If / #EndIf conditional compilation directives, but only in a very limited form.
    The #Const directive could be just what you need, except for one thing: you cannot use a constant declared with it in anything except an #If expression.
    (If you're after more detail, check out

    Another similar thing is normal Consts (Const blah As Integer = 12), but they can only replace variables - not code.

    To create different code text based on a const, you may need to build a string and tell the runtime to compile and execute it. I don't think .NET really
    supports this, but if you are desperate it could be worth looking into.

    PS: If anyone out there can contradict me, I would be overjoyed - macro substituion can be a _very_ useful tool.

    Expert Comment

    Oops, sorry - I answered a bit soon there. I just found this:
    If you are interested in executing a runtime-built string, you might want to take a look.

    A warning, however: code built up from strings at runtime can be a nightmare to debug, so if you can possibly do without this feature it's probably worth avoiding it.

    Author Comment

    What I really want to do is issue a call with the name of the function called in a variable. I was hoping for the macro substitution so that I could do this:

    MyFunc = "TheRealFucntionName"

    Call &MyFunc()

    which should be translated as

    Call TheRealFuntionName()

    I REALLY need to use control arrays like we were able to in VB6, so I worked out a way to do it thus far:
    It works except that all the controls (buttons in my example) in the array need to have a select case to handle EACH button in the one event handler that is executed when any button in the array is clicked. The handler works fine catching all the button clicks, but you are stuck in it unless you can define a case to do something for each button dynamically. You can dynamically add buttons at run time by redimming the button array and the button info array  but you can't dynamically add more buttons at run time than you have cases in the handler. I have an array of buttons and an parallel array of button information that includes button index and the function to be called. I just cant get that function called without the macro substitution.

    Is there a better way to do this?


    Author Comment

    Let me rephrase part of that. You CAN add more buttons than you have cases in the handler, its just that you cant dynamically add cases, so again you are stuck in the handler.

    Accepted Solution


    How is the button function stored in the button info array? Is it just the name?
    You could store a function pointer ('delegate' in .NET terminology) in each button info object instead.

    Basically, you declare a Delegate type, which defines the parameters and return type of the functions you want to store.
    Then, you can declare variables (or class members) of that type, through which you can call the original function.

    Look up Delegate in the MSDN docs for an example - there are also links to the AddressOf operator and an Events and Delegates overview,
    which look like they could be helpful.

    Feel free to ask for clarification if something isn't clear.  : )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    TextBlock Stops Updating 2 48
    SPLUNK REST  API call to Splunk to create and index? 2 39
    noX challenge 17 52
    nestparen challenge 4 33
    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now