Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

500pts: Using methods of a form/control if i have the name in a string?

Hi there,

I wonder if anyone can help?

I would like to beable to add to array or similar the names of certain controls and forms...

Then at a later stage i need to be able to call a method name, is this possible .. so say if i had

"frmTest"
"TestControl"

both are different and are in a string... but i need to be able to do something like

"frmTest".CommonMethod();

Any ideas or i could achieve this?

Thanks in advance

Ian
0
ianinspain
Asked:
ianinspain
3 Solutions
 
anyoneisCommented:
What is CommonMethod? Is it part of the Control interface? Or is it something you have added to your controls/forms? Is there a common type (base class or interface) which includes CommonMethod? If so, you can just keep a reference to all of these controls and forms in a hashtable, using the name as a key.

You can't just call a method on a name - you have to have an instance of the form or control before you can call the method.

Perhaps if you describe your scenario a bit more.

David
0
 
sammartinCommented:
Hi Ian

Does that mean the CommonMethod() is a static method of the types listed?

What are you trying to achieve?

Cheers,

Sam
0
 
jorgeariasCommented:
I think you are trying to do something with refection.

First, you can use the string "frmTestControl" to create an instance or call some method (If is static.)
Assembly current =  System.Reflection.Assembly.GetExecutingAssembly();
object instance = current.CreateInstance("namespace.nameofclase");

Then you can call the method you want with MethodInfo like this.
MethodInfo method = instance.GetType().GetMethod("AnyMethod")

To call the method you just do:
method.Invoke(instance).

You can also, user an interface to previously know the methods in the instance.

I hope it helps you.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sammartinCommented:
To give you an example Ian

consider the following type:

      public class InvokeExample
      {
            public static void CommonMethod()
            {
                  // do stuff here
            }
      }

If you're trying to call static members of types defined in a file/array etc then this is how you could do it.
myTypes would be the array of types you're looking for.
commonMethodName is the method it's looks for by itterating though the methods for each type.

If the type or method name is not found, nothing will happen.  
From memory there may be a way of finding a type without ittierating through the array returned by a.GetTypes()

You should change the Invoke call to include method parameters if required.

      string[] myTypes = new string[] { "InvokeExample" };
      string commonMethodName = "CommonMethod";

      System.Reflection.Assembly a = System.Reflection.Assembly.GetCallingAssembly();
      Type[] types = a.GetTypes();

      foreach(string myType in myTypes)
      {
            foreach(Type type in types)
            {
                  if (type.Name == myType)
                  {
                        System.Reflection.MethodInfo[] methods = type.GetMethods();

                        foreach(System.Reflection.MethodInfo method in methods)
                        {
                              if (method.Name == commonMethodName && method.IsStatic)
                              {
                                    method.Invoke(null,null);
                              }
                        }
                  }
            }
      }

Hope that helps.

Sam
0
 
sammartinCommented:
and like in jorgearias' example, you could use the type.GetMehod() instead of type.GetMethods() depending on whether you're called lots or just one method from the type.

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version are you working with?

If you have C# 2005 then you can iterate the Forms collection and look for a match in the form name property.

Then you can use Reflection as the others mentioned to run the specific method in the particular instance.
0
 
ianinspainAuthor Commented:
i am only working with version 2003 :-(

Ian
0
 
ianinspainAuthor Commented:
I am assigning points now because it seems to be working .. thank you

Ian
0
 
sammartinCommented:
So am I Ian.

Are the controls/forms you want to invoke methods on instantiaed objects or Types?

If you want to keep an array of objects to involke methods on, instead of holding an array of strings, hold an array of objects.

i.e.

myArray.Add(myForm);
myArray.Add(myControl);

then for example
foreach(object instance in myArray)
{
   MethodInfo method = instance.GetType().GetMethod("AnyMethod")
   method.Invoke(instance);
}

Depends on what you're trying to do.

HTH
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now