ianinspain
asked on
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
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
Hi Ian
Does that mean the CommonMethod() is a static method of the types listed?
What are you trying to achieve?
Cheers,
Sam
Does that mean the CommonMethod() is a static method of the types listed?
What are you trying to achieve?
Cheers,
Sam
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i am only working with version 2003 :-(
Ian
Ian
ASKER
I am assigning points now because it seems to be working .. thank you
Ian
Ian
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().GetMeth od("AnyMet hod")
method.Invoke(instance);
}
Depends on what you're trying to do.
HTH
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().GetMeth
method.Invoke(instance);
}
Depends on what you're trying to do.
HTH
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