Macro Substitution in VB.Net

Posted on 2005-04-06
Medium Priority
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
  • 3
  • 2

Expert Comment

ID: 13723347
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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vadirif.asp).

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

ID: 13723370
Oops, sorry - I answered a bit soon there. I just found this: http://www.thecodeproject.com/dotnet/evaluator.asp
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

ID: 13731721
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

ID: 13731909
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

aihtdikh earned 200 total points
ID: 13733211

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.  : )

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Loops Section Overview

809 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