473,769 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("My nameSpace.Class Name", false);
obj = (IMyInterface )Activator.Crea teInstance(typ) ;

public class MyUserControl1: UserControl, IMyInterface
{
public void InterfaceMethod 1()
{}
public void InterfaceMethod 2()
{}
.....
}

public class MyUserControl2 : UserControl, IMyInterface
{
public void InterfaceMethod 1()
{}
public void InterfaceMethod 2()
{}
.....
}
Thanks
Apr 10 '07 #1
4 4128
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.co m

"sippyuconn " <si********@new sgroup.nospamwr ote in message
news:D9******** *************** ***********@mic rosoft.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("My nameSpace.Class Name", false);
obj = (IMyInterface )Activator.Crea teInstance(typ) ;

public class MyUserControl1: UserControl, IMyInterface
{
public void InterfaceMethod 1()
{}
public void InterfaceMethod 2()
{}
....
}

public class MyUserControl2 : UserControl, IMyInterface
{
public void InterfaceMethod 1()
{}
public void InterfaceMethod 2()
{}
....
}
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("My nameSpace.Class Name", false);
Object obj = Activator.Creat eInstance(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,IMy Interface
{
public UserControl1()
{
InitializeCompo nent();
}
// implement the interface explicitly
void IMyInterface.Me thodofInterface ()
{
MessageBox.Show ("mehtod of Interface");
}
}

interface IMyInterface
{
void MethodofInterfa ce();
}

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

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,IMy Interface
{
// implement the interface implicitly
public void MethodofInterfa ce()
{
MessageBox.Show ("method of Interface: implicitly");
}
}
// invoke the interface method directly
uc.MethodofInte rface();
// or through the interface
((IMyInterface) uc).MethodofInt erface();

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,IMy Interface
{
public void MethodofUserCon trol1()
{
MessageBox.Show ("method of UserControl1");
}
....
}

We could invoke the 'MethodofUserCo ntrol' and 'MethodofInterf ace' methods
using reflection as follows:

Type typ =
Assembly.GetExe cutingAssembly( ).GetType("Wind owsMediaPlayer. UserControl1",
false);
object obj = Activator.Creat eInstance(typ);

MethodInfo mi = typ.GetMethod(" MethodofUserCon trol1");

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(" MethodofInterfa ce");
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.Met hodofInterface( );
}

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3039
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
11
2168
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
3
1423
by: ahaupt | last post by:
Hi all, At the moment we use static methods for retrieving data: Ex. DataTable supplierDT = DAC.Supplier.GetAll(); DataTable customerDT = DAC.Customer.GetAll(); The reason we use static methods is that it's not really worth the
5
3198
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. 1. I can create a common module like common.vb in my project and put all the functions in there. 2. Create a utility class and create the common functions as shared
1
2054
by: skootr | last post by:
I have a Public Interface defined in a class module. I also have a form that implements that interface After building the solution, I added an Inherited Form (inherited from the above-mentioned form) to the project via the IDE However, when I drop down the "(base class events)" list, I can't find the methods defined in the interface. I know the interface has been inherited because if I enter an "Implements" statement in the derived...
3
5483
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile. (You must have a <% Register %> so it will see it) You will get TWO warnings per class like:
5
1280
by: Shak | last post by:
Hi all. I was led to believe that static methods were not inherited by their subclasses (and since that makes sense, rightly so). However, a subclass I've written is using it's (abstract) superclass's operators. How can that be when operators are always static? Is it a special case? It's handy, sure, but doesn't it "break" the language somehow? Or isn't this actually inheritance at play here?
13
2507
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic experience. We employ some parts of XP such as pair programming, and this should help. Other than books, does anyone have any suggestions? His skill set is pretty antiquated and we need to get him up to speed as quickly as possible so any suggestions...
19
2236
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.