Multiple Inhertance solution
-
Hi All, let's suppose
class A
{
int iA1{ get; set; }
int iA2{ get; set; }
}
class B
{
int iB1{ get; set; }
int iB2{ get; set; }
}
class C
{
int iC1{ get; set; }
int iC2{ get; set; }
}Now i want make type ABC1 for List<<ABC1> and this ABC1 contain iA1,iB1,iC1 and iC2. But bellow code is not working...plz suggest me, i just want List.
public interface IABC1
{
A a { get; set; }
B b { get; set; }
C c { get; set; }
}
public class ABC1:IABC1
{
#region IABC1 Members
....
...
}Thanks,
-
Hi All, let's suppose
class A
{
int iA1{ get; set; }
int iA2{ get; set; }
}
class B
{
int iB1{ get; set; }
int iB2{ get; set; }
}
class C
{
int iC1{ get; set; }
int iC2{ get; set; }
}Now i want make type ABC1 for List<<ABC1> and this ABC1 contain iA1,iB1,iC1 and iC2. But bellow code is not working...plz suggest me, i just want List.
public interface IABC1
{
A a { get; set; }
B b { get; set; }
C c { get; set; }
}
public class ABC1:IABC1
{
#region IABC1 Members
....
...
}Thanks,
OK i find it ...just make the public.
class A
{
public int iA1{ get; set; }
public int iA2{ get; set; }
}Is it better way for multiple inheritance? Thanks,
-
OK i find it ...just make the public.
class A
{
public int iA1{ get; set; }
public int iA2{ get; set; }
}Is it better way for multiple inheritance? Thanks,
zeeShan anSari wrote:
Is it better way for multiple inheritance?
Well, it isn't multiple inheritance, but it may suit your needs better. And a shameless plug: Implanting Common Code in Unrelated Classes[^]
-
OK i find it ...just make the public.
class A
{
public int iA1{ get; set; }
public int iA2{ get; set; }
}Is it better way for multiple inheritance? Thanks,
C#/.Net doesn't have multiple inheritance. It has multiple implementation (i.e. a class can implement multiple interfaces), so if the things you want to 'inherit' are simple enough, you can implement an interface and re-implement the methods), or alternatively you can switch from 'multiple is-a' (inheritance, which can't be done) to 'multiple has-a' (composition), which is essentially what you've done here. You can fake multiple inheritance quite well thus:
class Multi { // : A, B
private A a;
private B b;Multi(string argForA, int argForB){
a = new A(argForA);
b = new B(argForB);
}public static implicit operator A(Multi m) { return m.a; }
public static implicit operator B(Multi m) { return m.b; }
}You can use that class as if it were an A or a B in variable assignments, foreach constructs and anywhere where an implicit cast gets used. However, you can't use a reverse cast (i.e. (Multi)(A)m won't work), the reference you get from a cast isn't actually the same object you started with, and putting it in collections would therefore be dodgy. I don't really recommend you do that, though.
-
Hi All, let's suppose
class A
{
int iA1{ get; set; }
int iA2{ get; set; }
}
class B
{
int iB1{ get; set; }
int iB2{ get; set; }
}
class C
{
int iC1{ get; set; }
int iC2{ get; set; }
}Now i want make type ABC1 for List<<ABC1> and this ABC1 contain iA1,iB1,iC1 and iC2. But bellow code is not working...plz suggest me, i just want List.
public interface IABC1
{
A a { get; set; }
B b { get; set; }
C c { get; set; }
}
public class ABC1:IABC1
{
#region IABC1 Members
....
...
}Thanks,
As noted multiple inheritance isn't possible but you can chain your inheritance
class A
{
int iA1{ get; set; }
int iA2{ get; set; }
}
class B :A
{
int iB1{ get; set; }
int iB2{ get; set; }
}
class C :B
{
int iC1{ get; set; }
int iC2{ get; set; }
}C c = new C();
c.iA1 = 0;
c.iA2 = 1;
c.iB1 = 2;
c.iB2 = 3;
..."You get that on the big jobs."
-
C#/.Net doesn't have multiple inheritance. It has multiple implementation (i.e. a class can implement multiple interfaces), so if the things you want to 'inherit' are simple enough, you can implement an interface and re-implement the methods), or alternatively you can switch from 'multiple is-a' (inheritance, which can't be done) to 'multiple has-a' (composition), which is essentially what you've done here. You can fake multiple inheritance quite well thus:
class Multi { // : A, B
private A a;
private B b;Multi(string argForA, int argForB){
a = new A(argForA);
b = new B(argForB);
}public static implicit operator A(Multi m) { return m.a; }
public static implicit operator B(Multi m) { return m.b; }
}You can use that class as if it were an A or a B in variable assignments, foreach constructs and anywhere where an implicit cast gets used. However, you can't use a reverse cast (i.e. (Multi)(A)m won't work), the reference you get from a cast isn't actually the same object you started with, and putting it in collections would therefore be dodgy. I don't really recommend you do that, though.
I thought about this some more and you can make the fakery even better, as long as you can actually inherit from A and B (which is a requirement for multiple inheritance when it's supported, obviously):
class Multi { // : A, B
private A a;
private B b;Multi(string argForA, int argForB){
a = new HostedA(this, argForA);
b = new HostedB(this, argForB);
}public static implicit operator A(Multi m) { return m.a; }
public static implicit operator B(Multi m) { return m.b; }public static implicit operator Multi(A a) { return ((HostedA)a).host; }
public static implicit operator Multi(B b) { return ((HostedB)b).host; }private class HostedA : A {
internal Multi host;
internal HostedA(Multi host, string argsForA) : base(argsForA) {
this.host = host;
}
}private class HostedB : B {
internal Multi host;
internal HostedB(Multi host, int argsForB) : base(argsForB) {
this.host = host;
}
}
}Those cast-back operators will fail if you give it the wrong sort of A or B (i.e. one that isn't a cast-out of a Multi), but that's correct (though the exception message might not be quite right). You can override behaviour 'inherited' from A and B in HostedA and HostedB, and as inner classes they have access to their host's private state and methods. If you want Multi itself to be virtual and inheritable, you can virtualise the creation of the instances of A and B and make the hosted classes visible for inheritance:
class Multi { // : A, B
private A a;
private B b;Multi(string argForA, int argForB){
a = ConstructA();
b = ConstructB();
}protected virtual HostedA ConstructA(string argForA) { return new HostedA(this, argForA); }
protected virtual HostedB ConstructB(int argForB) { return new HostedB(this, argForB); }public static implicit operator A(Multi m) { return m.a; }
public static implicit operator B(Multi m) { return m.b; }public static implicit operator Multi(A a) { return ((HostedA)a).Host; }
public static implicit operator Multi(B b) { return ((HostedB)b).Host; }protected class HostedA : A {
public Multi Host { get; private set; }
public HostedA(Multi host, string argsForA) : base(argsForA) {
this.Host = host;
}
}protected class HostedB : B {
public Multi Host { get; private set; }
public HostedB(Multi host, int argsForB) : base(argsForB) {
this.Host = host;
}
}
}To override some of Multi's 'A' behaviour, create a new inner subclass and overrid