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}

[168 byte] By [JRQ] at [2007-12-22]
# 1

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?

ahmedilyas at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
The Microsoft.VisualBasic.dll assembly has the CallByName method.

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

nobugz at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
question - why would you want to do this anyway? :-)
ahmedilyas at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
No problem as I'm already using the assembly to use the friendlier Collection class.
JRQ at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 5

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.

JRQ at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 6
Cool!

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

nobugz at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 7

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

AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 8

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.

JRQ at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 9
A results-oriented developer if ever I heard of one. Visual Studio 2005 could use a fix or two but I think the features outweigh the occassional bug (which man not even exist on your computer). You should definately take the express editions for a spin if nothing else :)
AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 10
And it does take a "results-oriented" developer to wait for the bugs getting flushed out. The .NET 2.0 class library is an absolute class-act. The IDE is a buggy mess. I've been poking at ways to get to use the CLR but without the IDE. It is a pretty thin shell over the SDK tools when it comes to editing and compiling. Not when it comes to designing and debugging. I think I've got time to wait for SP1. Latest update: originally supposed to be available in summer, now pushed back to Q3. Uh oh...
nobugz at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...
# 11
I've certainly been in situations where I've been either unable or unwilling to use the designers or IDE. MSBuild is great in that respect because you can compile your projects from the command line. I used to use it a lot for on-site hotfixes to minor portions of code. While the v2.0 class library does improve in many areas there do seem to be areas where conventions are ignored or opportunities for overloads or generics aren't used. Q3 is here and going. Maybe they'll wait for the big LINQ Release?
AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual C#,Visual C# General...