Solved

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

Posted on 2006-11-23
9
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 11

Expert Comment

by:anyoneis
ID: 18004292
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
ID: 18004296
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
ID: 18004345
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
Industry Leaders: 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!

 
LVL 1

Assisted Solution

by:sammartin
sammartin earned 200 total points
ID: 18004357
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
 
LVL 1

Expert Comment

by:sammartin
ID: 18004365
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 18004612
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
ID: 18029613
i am only working with version 2003 :-(

Ian
0
 

Author Comment

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

Ian
0
 
LVL 1

Expert Comment

by:sammartin
ID: 18029665
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

710 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