472,096 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Best way to access Inherited Methods

Hi

I have a user control that is designed as below.
I am creating these User Controls Dynamically in another form. They are
multiple types of User Controls all with a common Interface so I can use a
Factory to create them all in a generic way.

The problem is that if I create a IMyInterface - I cannot access the
derived UserControl Methods.
I need to access the Methods derived from UserControl and also the Interface
Methods

Do I need to create the control as multiple types or can I cast from 1 type
to another? I don't want to create as the 3rd line below because that is not
generic and I would need a line for all the types of UserControls

UserControl frm = new MyUserControl1 (); ----Get only UserControl
Methods
IMyInterface frm = new MyUserControl1 (); ----Get only IMyInterface
Methods
MyUserControl1 frm = new MyUserControl1(); ----Get all Methods
This is how I actually am creating the Controls - then add to form Controls
Collection
obj = new Object() as IMyInterface ;
Type typ = ass.GetType("MynameSpace.ClassName", false);
obj = (IMyInterface )Activator.CreateInstance(typ);

public class MyUserControl1: UserControl, IMyInterface
{
public void InterfaceMethod1()
{}
public void InterfaceMethod2()
{}
.....
}

public class MyUserControl2 : UserControl, IMyInterface
{
public void InterfaceMethod1()
{}
public void InterfaceMethod2()
{}
.....
}
Thanks
Apr 10 '07 #1
4 4015
sippyuconn,

If you have the need to access the methods on the underlying class
implementation which are not exposed through the interface, you have to
either use reflection (if you don't have access to the type because you have
a reference) or cast to the type directly and then use that.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sippyuconn" <si********@newsgroup.nospamwrote in message
news:D9**********************************@microsof t.com...
Hi

I have a user control that is designed as below.
I am creating these User Controls Dynamically in another form. They are
multiple types of User Controls all with a common Interface so I can use a
Factory to create them all in a generic way.

The problem is that if I create a IMyInterface - I cannot access the
derived UserControl Methods.
I need to access the Methods derived from UserControl and also the
Interface
Methods

Do I need to create the control as multiple types or can I cast from 1
type
to another? I don't want to create as the 3rd line below because that is
not
generic and I would need a line for all the types of UserControls

UserControl frm = new MyUserControl1 (); ----Get only UserControl
Methods
IMyInterface frm = new MyUserControl1 (); ----Get only IMyInterface
Methods
MyUserControl1 frm = new MyUserControl1(); ----Get all Methods
This is how I actually am creating the Controls - then add to form
Controls
Collection
obj = new Object() as IMyInterface ;
Type typ = ass.GetType("MynameSpace.ClassName", false);
obj = (IMyInterface )Activator.CreateInstance(typ);

public class MyUserControl1: UserControl, IMyInterface
{
public void InterfaceMethod1()
{}
public void InterfaceMethod2()
{}
....
}

public class MyUserControl2 : UserControl, IMyInterface
{
public void InterfaceMethod1()
{}
public void InterfaceMethod2()
{}
....
}
Thanks

Apr 10 '07 #2
Hi,

I agree with Nicholas.

If you use the following statement to declare an object:
IMyInterface obj;

to access the methods of the underlying implementation class, you should
either use reflection, which is a generic way, or cast to the type
directly, which is not generic.

In addition, if possible, you may create a base UserControl and add some
methods to it. You derive other UserControls in the project from the base
UserControl. Then you could cast the object to the type of the base
UserControl, regardless of what the exact type the object is.

Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 11 '07 #3
Linda - Thanks for the info

One quick question after getting the class as below
How do I use Reflection to switch to the different Inherited Types ie
UserControl and
IMyInterface

Type typ = ass.GetType("MynameSpace.ClassName", false);
Object obj = Activator.CreateInstance(typ);
my class
public class MyUserControl1: UserControl, IMyInterface
{

}
Thanks again
"Linda Liu [MSFT]" wrote:
Hi,

I agree with Nicholas.

If you use the following statement to declare an object:
IMyInterface obj;

to access the methods of the underlying implementation class, you should
either use reflection, which is a generic way, or cast to the type
directly, which is not generic.

In addition, if possible, you may create a base UserControl and add some
methods to it. You derive other UserControls in the project from the base
UserControl. Then you could cast the object to the type of the base
UserControl, regardless of what the exact type the object is.

Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 11 '07 #4
Hi,

Thank you for your prompt response.

As we all know, if we implment an interface explicitly, we must convert the
underlying implementation class's instance to the type of the interface and
then invoke the methods defined in the interface. The following is a sample:

public partial class UserControl1 : UserControl,IMyInterface
{
public UserControl1()
{
InitializeComponent();
}
// implement the interface explicitly
void IMyInterface.MethodofInterface()
{
MessageBox.Show("mehtod of Interface");
}
}

interface IMyInterface
{
void MethodofInterface();
}

// define an instance of UserControl1
UserControl1 uc = new UserControl1();
// we must cast uc to the type of IMyInterface to invoke the
'MethodofInterface' method
((IMyInterface)uc).MethodofInterface();

If we implement the interface implicitly, we could invoke the interface
methods either through the implementation class's instance directly, or
through the interface. The following is a sample.

public partial class UserControl1 : UserControl,IMyInterface
{
// implement the interface implicitly
public void MethodofInterface()
{
MessageBox.Show("method of Interface: implicitly");
}
}
// invoke the interface method directly
uc.MethodofInterface();
// or through the interface
((IMyInterface)uc).MethodofInterface();

Similarly, if we implement an interface explicitly, we couldn't invoke the
interface methods using the reflection on the implementation class's
instance. Reflection is only a technique to determine the type of an object
at run time, which is very useful when we couldn't determine the type of an
object at compile time. Using reflection, we could get the properties and
invoke the methods of the object at run time.

To demonstrate an example, I add a method into the UserControl1 class as
follows:

public partial class UserControl1 : UserControl,IMyInterface
{
public void MethodofUserControl1()
{
MessageBox.Show("method of UserControl1");
}
....
}

We could invoke the 'MethodofUserControl' and 'MethodofInterface' methods
using reflection as follows:

Type typ =
Assembly.GetExecutingAssembly().GetType("WindowsMe diaPlayer.UserControl1",
false);
object obj = Activator.CreateInstance(typ);

MethodInfo mi = typ.GetMethod("MethodofUserControl1");

if (mi != null)
{
mi.Invoke(obj, new object[] { });
}
// if the UserControl1 implements the interface implicitly, we could
invoke the interface's method as follows; otherwise, mi below returns null
mi = typ.GetMethod("MethodofInterface");
if (mi != null)
{
mi.Invoke(obj, new object[] { });
}
// alternatively, we could cast the object to the type of the interface
and then invoke the interface method directly
IMyInterface myInterface = obj as IMyInterface;
if (myInterface != null)
{
myInterface.MethodofInterface();
}

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Apr 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by D Witherspoon | last post: by
11 posts views Thread by Noah Coad [MVP .NET/C#] | last post: by
3 posts views Thread by Wayne Brantley | last post: by
5 posts views Thread by Shak | last post: by
19 posts views Thread by jan.loucka | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.