Solved

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

Posted on 2006-11-23
9
255 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
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!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

631 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