Macro Substitution in VB.Net

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?

ThanborAsked:
Who is Participating?
 
aihtdikhCommented:
Hmm...

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.  : )
0
 
aihtdikhCommented:
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.
0
 
aihtdikhCommented:
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.
0
 
ThanborAuthor Commented:
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?

0
 
ThanborAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.