Solved

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

Posted on 2006-11-23
9
239 Views
Last Modified: 2010-04-16
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
Comment
Question by:ianinspain
9 Comments
 
LVL 11

Expert Comment

by:anyoneis
Comment Utility
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
 
LVL 1

Expert Comment

by:sammartin
Comment Utility
Hi Ian

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

What are you trying to achieve?

Cheers,

Sam
0
 
LVL 1

Accepted Solution

by:
jorgearias earned 200 total points
Comment Utility
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
 
LVL 1

Assisted Solution

by:sammartin
sammartin earned 200 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:sammartin
Comment Utility
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
Comment Utility
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
 

Author Comment

by:ianinspain
Comment Utility
i am only working with version 2003 :-(

Ian
0
 

Author Comment

by:ianinspain
Comment Utility
I am assigning points now because it seems to be working .. thank you

Ian
0
 
LVL 1

Expert Comment

by:sammartin
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now