I have a class with 2 interfaces, and I have some superclasses with subclasses, I would like the superclasses to inherit both interfaces. if I just reference the class the interfaces its in, will it work? ie SuperClass : Myinterfaces
here is the class with the interfaces
public class Myinterfaces
{
public interface IBakeable
{
int OvenTemp { get; }
}
public interface IAccounting
{
int Cost { get; }
}
public enum Colors
{
red = 1,
blue,
yellow
}
}
and heres an example of the superclass
public class CeramicsSuperClass : Myinterfaces
{
public string ItemName { get; set; }
public int Cost { get; set; }
public int OvenTemp { get; set; }
}
public class Vases : CeramicsSuperClass
{
private int _BaseDiam;
public Vases(int diam)
{
_BaseDiam = diam;
}
}
You are doing in a wrong way to implement multi-interfaces for a class, try this instead:
public class CeramicsSuperClass : IBakeable, IAccounting {
public string ItemName { get; set; }
public int Cost { get; set; }
public int OvenTemp { get; set; }
}
A class can inherit from only another class but it can implement as many interfaces as possible. When a class inherits from another class and implement some interface, the base class should be listed first, then the interfaces go after like this:
//class A inherits from class B and implements 2 interfaces IC and ID
public class A : B, IC, ID {
//...
}
Simple answer:
You can inherit mulitple interfaces, not multiple classes.
public interface InterfaceA
{
string PropertyA {get;}
}
public interface InterfaceB
{
string PropertyB {get;}
}
public abstract class BaseClassForOthers : InterfaceA, InterfaceB
{
private string PropertyA {get; private set;}
private string PropertyA {get; private set;}
public BaseClassForOthers (string a, string b)
{
PropertyA = a;
PropertyB = b;
}
}
public class SubClass : BaseClassForOthers
{
public SubClass (string a, string b)
: base(a, b)
{
}
}
may be looking here will get you in the general direction (msdn link about interface usage):
http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx
Related
I have a question that is not a direct coding problem, so I am not sure this is the place to ask. If not please advise on where better to ask.
I have a .Net assembly exposed to COM. It uses a base class and several derived classes, all exposed via interfaces.
Base class:
public abstract class BaseFoo : IBaseFoo
{
public abstract int X { get; }
public abstract int Y { get; }
}
with interface:
public interface IBaseFoo
{
int X { get; }
int Y { get; }
}
Derived classes are COM-visible and add additional functionality.
[ComVisible(true)]
[Guid("123-456-789")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IFooA))]
public class FooA : BaseFoo, IFooA
{
public override int X { get; }
public override int Y { get; }
public string Z { get; set; }
}
The interface is exposed to COM:
[ComVisible(true)]
[Guid("321-654-987")]
public interface IFooA : IBaseFoo
{
string Z { get; set; }
}
At this point, people who have worked with COM will notice a problem. IFooA will be exposed to COM, but it will not expose anything from IBaseFoo, because inherited interfaces do not make it to COM.
To overcome that, I did this:
[ComVisible(true)]
[Guid("321-654-987")]
public interface IFooA : IBaseFoo
{
new int X { get; }
new int Y { get; }
public string Z { get; set; }
}
Which finally brings me to my question: I do not like that new in there. It feels wrong overriding the inherited interface. Would it be better to create a separate interface IComFooA, have FooA inherit from it, and expose that interface to COM, the clean up IFooA so as to be just .Net? In essence: would you advise the setup sketched above or the one sketched below, and why?
[ComVisible(true)]
[Guid("123-456-789")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IComFooA))]
public class FooA : BaseFoo, IFooA, IComFooA
{
public override int X { get; }
public override int Y { get; }
public string Z { get; set; }
}
with IComFooA being:
[ComVisible(true)]
[Guid("321-654-987")]
public interface IComFooA
{
int X { get; }
int Y { get; }
public string Z { get; set; }
}
And IFooA being just:
public interface IFooA : IBaseFoo
{
public string Z { get; set; }
}
I want to inherit some properties that are the same in 2 classes using interface or abstract class; I don't want to declare variable in child classes.
Is it possible to do this?
Parent Class:
interface IError
{
DateTime GETDATE { get; set; }
string ERROR_NUMBER { get; set; }
string ERROR_SEVERITY { get; set; }
string ERROR_STATE { get; set; }
string ERROR_PROCEDURE { get; set; }
string ERROR_LINE { get; set; }
string ERROR_MESSAGE { get; set; }
}
Child :
public class Business
{
public Business()
{
this.Id = -1;
this.Message = "";
}
public int Id { get; set; }
public string Message { get; set; }
}
Another child:
public class DbTools
{
//Another Class
}
I need parent properties in this two classes, but if change code like below, I should use another variable that I don't want to:
public class Business : IError
{
private DateTime m_GetDATE;//I don't want to declare another variable
public DateTime GETDATE
{
get { return m_GetDATE; }
set { GETDATE = m_GetDATE; }
}
}
Sounds like you want an abstract base class:
public abstract class ErrorBase : IError
{
public DateTime GETDATE { get; set; }
//...ditto all the other IError properties
}
Then you can just inherit that:
public class Business : ErrorBase
{
// all the ErrorBase properties are inherited
}
You could also make the base class properties virtual just in case you need to override them in the derived classes at any point.
The other thing you might consider is composing rather than inheriting. It doesn't seem all that natural that Business and DbTools should be inheriting from the same base class, so instead have a (non-abstract) ErrorBase class that implements IError and have Business and DbTools have an Error property:
public class ErrorBase : IError
{
public DateTime GETDATE { get; set; }
//...ditto all the other IError properties
}
public interface IHaveError
{
ErrorBase Error { get; set; }
}
public class Business : IHaveError
{
public ErrorBase { get; set; }
}
As a bonus, you could make IHaveError actually be IHaveErrors and have a List<ErrorBase> property allowing your objects to hold more than one error, which might be useful sometimes.
I have a class that has a property, and I want to force that this property is of a Type that inherits from a base type
public abstract class BasePropertyClass
{
public string A { get; set;}
}
public class MyPropClass : BasePropertyClass
{
public string B { get; set;}
}
public class MyOtherPropClass : BasePropertyClass
{
public string C { get; set;}
}
public class MyClass
{
public MyPropClass MyPropThatMustInheritBasePropertyClass { get; set;}
}
public class MyOtherClass
{
public MyOtherPropClass MyPropThatMustInheritBasePropertyClass { get; set;}
}
So, how can I make an Interface-ish solution that MyClass and MyOtherClass must have a property MyPropThatMustInheritBasePropertyClass that must have BasePropertyClass as base type
You can make your class generic and add a constraint
public class MyClass<T> where T : BasePropertyClass
{
public T MyPropThatMustInheritBasePropertyClass { get; set;}
}
To better explain #Selman22 answer, you can do something like this:
public abstract class ConstrainedClass<T>
where T: BasePropertyClass
{
public T MyPropThatMustInheritBasePropertyClass { get; set;}
}
In this way, if you declare the other to classes like this:
public class MyClass:ConstrainedClass<MyPropClass>
{
}
public class MyOtherClass:ConstrainedClass<MyOtherPropClass>
{
}
you will obtain the same classes as you declared in your question, plus you have the constraint in the property.
If your classes already inerhits from another class, you need an additional step:
public interface ConstrainedInterface<T>
where T: BasePropertyClass
{
T MyPropThatMustInheritBasePropertyClass { get; set;}
}
and after you must explicitly define the two properties:
public class MyClass : ConstrainedInterface<MyPropClass>
{
public MyPropClass MyPropThatMustInheritBasePropertyClass { get; set;}
}
and
public class MyOtherClass : ConstrainedInterface<MyOtherPropClass>
{
public MyOtherPropClass MyPropThatMustInheritBasePropertyClass { get; set;}
}
You could just use your base type:
public class MyClass
{
public BasePropertyClass Property1 { get; set;}
}
public class MyOtherClass
{
public BasePropertyClass Property2 { get; set;}
}
since both MyPropClass and MyOtherPropClass inherits the same base type, BasePropertyClass.
This way you could assign to either Property1 or Property2 and object of type MyPropClass or MyOtherPropClass.
You can't directly, because the implemented property must be exactly of the type specified in the interface.
Alternative to #Selman's generic solution, you can do so using a backing field:
public interface MyClassInterface
{
BasePropertyClass MyPropThatMustInheritBasePropertyClass { get; set;}
}
public class MyClass : MyClassInterface
{
private MyPropClass _actualProperty;
public BasePropertyClass MyPropThatMustInheritBasePropertyClass
{
get { return _actualProperty; }
set { _actualProperty = (MyPropClass)value; }
}
}
This assumes the not specified requirement "I want to have a member of the derived type in the implementing class", which MyPropClass _actualProperty fulfills.
This will not ensure type safety for the setter, which may throw a cast exception when setting a variable of the wrong type (in this case MyOtherPropClass).
Interface A
{
string x {get;set;}
IEnumarable<InterfaceB> DetailList { get; set; }
}
Interface B
{
int z;
int y;
}
Class B :Interface B
{
implements z;
implements y;
}
Class A :Interface A
{
implements x;
IEnumarable<ClassB> DetailList {get;set;} // This line is giving trouble.
}
Is this code violating OO concept. I thought if I derive ClassB from InterfaceB then I can use ClassB in my ClassA instead of InterfaceB. VS is not liking this, Its asking me to use InterfaceB instead of ClassB in ClassA.
Is there any other way to go about doing this.
I am willing to consider alternate designing options, I have some domain objects whose properties are defined by Interface A and each domain object would have corresponding object defined by interface B
e.g
concert(A) concertlocations(B)
comedyshow(A) comedyshowlocations(B)
Feel free to ask more questions if you think I am not being clear enough.
Thanks in Advance
You can this:
public interface InterfaceA<T> where T : InterfaceB
{
string x {get;set;}
IEnumerable<T> DetailList { get; set; }
}
public interface InterfaceB
{
int z { get; }
int y { get; }
}
public class ClassB : InterfaceB
{
public int z { get; private set; }
public int y { get; private set; }
}
public class ClassA : InterfaceA<ClassB>
{
public int z { get; private set; }
public string x { get; set; }
public IEnumerable<ClassB> DetailList {get;set;}
}
but I'm not sure this is desirable for you?
see here for more info: c# interface implemention - why does this not build?
Interface A just says that the IEnumarable<InterfaceB> DetailList { get; set; } has to be present in any class, that is implementing it, with exactly InterfaceB being the generic type for IEnumerable, and not one of it's implementaions.
Class B is less general than Interface B, therefore it is completely logical that it does not allow you to use it in such manner.
I have an object that implements two interfaces... The interfaces are:
public interface IObject
{
string Name { get; }
string Class { get; }
IEnumerable<IObjectProperty> Properties { get; }
}
public interface ITreeNode<T>
{
T Parent { get; }
IEnumerable<T> Children { get; }
}
such that
public class ObjectNode : IObject, ITreeNode<IObject>
{
public string Class { get; private set; }
public string Name { get; private set; }
public IEnumerable<IObjectProperty> Properties { get; set; }
public IEnumerable<IObject> Children { get; private set; }
public IObject Parent { get; private set; }
}
Now i have a function which needs one of its parameters to implement both of these interfaces. How would i go about specifying that in C#?
An example would be
public TypedObject(ITreeNode<IObject> baseObject, IEnumerable<IType> types, ITreeNode<IObject>, IObject parent)
{
//Construct here
}
Or is the problem that my design is wrong and i should be implementing both those interfaces on one interface somehow
public void Foo<T>(T myParam)
where T : IObject, ITreeNode<IObject>
{
// whatever
}
In C#, interfaces can themselves inherit from one or more other interfaces. So one solution would be to define an interface, say IObjectTreeNode<T> that derives from both IObject and ITreeNode<T>.
It's probably easiest to define an interface that implements both IObject and ITreeNode.
public interface IObjectNode<T> : IObject, ITreeNode<T>
{
}
Another option, in case you don't expect the above interface would be used often, is to make the method/function in question generic.
public void Foo<T>(T objectNode) where T : IObject, ITreeNode<IObject>
public void MethodName<TParam1, TParam2>(TParam1 param1, TParam2 param2)
where TParam1 : IObject
where TParam2 : ITreeNode<IObject>