Call Method by Name
How do you call(inside the class itself) a method of a class using its string name?
Something like:
for(n=0; n<10; n++) {"MyMethod" + n}
How do you call(inside the class itself) a method of a class using its string name?
Something like:
for(n=0; n<10; n++) {"MyMethod" + n}
I don't think I quite follow. Do you mean to call a method inside a class and execute that method?
if so...
this.MyMethod();
if you need to pass parameters then specify them in the brackets, example:
string myName = "JRQ";
this.DoShowName(myName);
does this help? if not - are you able to elaborate a bit more?
Hope it doesn't turn you off too much but it *is* the power of .NET to
have that available. To use it, you'll have to add a reference to the
assembly...
ahmedilyas wrote:
question - why would you want to do this anyway? :-)
This is nice to know when you as doing some sort of on-the-fly code and compilation.
Sometimes this is more readable than a 2 paged switch/case statement.
The J# assembly vjslib.dll has some neato stuff as well.
Highlights I see are java.math.BigInteger and BigDecimal for arbitrary
precision arithmetic, java.util.Random for advanced random number
generation and java.util.zip.ZipFile to handle .zip archives...
As happy as I am that you are open enough to reference the VB runtime I think that you have other options at your disposal. Most notably reflection, which is probably what the VB runtime library is calling into.
The Object.GetType method that all classes inherit gets a System.Type object describing that class. It has several members such as GetMethod that can be used to get objects of other types defined in the System.Reflection namespace that describe your classes properties, fields, methods, events, properties, and constructors. Once you get a MethodInfo object you can invoke it. Please refer to the MSDN Documentation for the these types and Reflection for a better idea of how to do it. It's fairly straightworward but I warn that when calling the GetMethods method it may be called for to use the longer overload and specify the right bitwise combination for the method you're trying to find as the other overload by default only finds public instance method, if I recall correctly.
As another note, have you .NET 2.0? If so what about the System.Collections.Generic collections do you find so difficult as to merit using the VB collection class? Are you a former VB6 developer? If so what motivated you to move to C#?
An alternative to Reflection would be an array of delegates, this is only really an option if all required methods share the same signature. Technically you could overcome this limitation still be defining the array as an array of System.Delegate or MultiCastDelegate which would lock you into using Delegate.DynamicInvoke but if the methods all have different signatures that was unavoidable. The only stressing factor here is that you may need to define different delegate types for each method signature rather than one for all.
Hope that gives you a clearer view of your options, back to VB Land with me :)
I'm still on VS2003. I'm waiting for VS2005 to mature before switching.
I'm "multi-lingual" I have no preference on what programming language to use when coding. If one language already implements what I need, I use it.