473,795 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enforce that a class can only inherit one interface

Hi

Is it possible in one way or another to enforce that a class only inherit
from one of the derived interfaces of a base interface of some kind. For
example.
public class MyClass2 : IMyInterface1 => OK
public class MyClass2 : IMyInterface1, IMyInterface2 => Error

Regards
/Niklas

public interface IMyInterfacsBas e
{
string MyMethod1();
}

public interface IMyInterface1 : IMyInterfacsBas e
{
int MyInt1{ get; }
}

public interface IMyInterface2 : IMyInterfacsBas e
{
int MyInt1{ get;}
int MyInt2{ get;}
}

public class MyClass2 : IMyInterface1, IMyInterface2
{
private int _MyInt1 = 4;
private int _MyInt2 = 5;

public int MyInt1
{
get { return _MyInt1; }
}

public int MyInt2
{
get { return _MyInt2; }
}

public string MyMethod1()
{
throw new NotImplementedE xception();
}
}
Feb 22 '06 #1
2 1877
Hi,

"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:5A******** *************** ***********@mic rosoft.com...
Hi

Is it possible in one way or another to enforce that a class only inherit
from one of the derived interfaces of a base interface of some kind. For


First of all, a class do not inherit an interface, it implement it. which is
different.

And no, there is no way I know of to especify that a class can implement at
the most one nterface.

If you substitute interface by class you can be sure of that, as C# ins a
single inheritance language.

why you want to do this anyway?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 22 '06 #2
"Niklas" <Ni****@discuss ions.microsoft. com> a écrit dans le message de news:
5A************* *************** **...icrosof t.com...

| Is it possible in one way or another to enforce that a class only inherit
| from one of the derived interfaces of a base interface of some kind. For
| example.
| public class MyClass2 : IMyInterface1 => OK
| public class MyClass2 : IMyInterface1, IMyInterface2 => Error

No, but you can delegate an interface to an aggregated object inside the
main class, so that each interface's behaviour is looked after separately,
even though the class implements more than one interface.

What are you really trying to achieve ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7559
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must be implemented. So if you have a class which inherits base class that inherts an interface, then your classes will have a standard. I suppose you can also check for interface at run time say when dll is loaded and see if it implememts whats...
10
2982
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
1
1616
by: HC | last post by:
I've stripped down my code to barebones to demonstrate (below). I have interfaces and classes which implement them. I force my generic collection to accept only the interface type. If I create an instance of the generic class using the class implemention of the interface I find it can't be converted to the interface version, and vice versa. This a pain with my current design. Can someone please advise on what best practice might be in...
3
2014
by: Matt F. | last post by:
I have an abstract class that about a dozen sub-classes inherit from. I want to enforce that each sub-class shadows an event in the abstract class, but can't quite figure out how to do this. Basically, all of the inherited classes deal with data in some way or another. I need to make sure that when the data is changed on any of those classes, they raise an event called "DataChanged", and have that enforced in some way from the abstract...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10164
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
10001
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...
1
7540
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
5437
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.