Solved

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

Posted on 2006-11-23
9
241 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

18 Experts available now in Live!

Get 1:1 Help Now