Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multiple Inhertance solution

Multiple Inhertance solution

Scheduled Pinned Locked Moved C#
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zeeShan anSari
    wrote on last edited by
    #1

    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,

    Z R 2 Replies Last reply
    0
    • Z zeeShan anSari

      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,

      Z Offline
      Z Offline
      zeeShan anSari
      wrote on last edited by
      #2

      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,

      P B 2 Replies Last reply
      0
      • Z zeeShan anSari

        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,

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        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[^]

        1 Reply Last reply
        0
        • Z zeeShan anSari

          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,

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          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.

          B 1 Reply Last reply
          0
          • Z zeeShan anSari

            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,

            R Offline
            R Offline
            RobCroll
            wrote on last edited by
            #5

            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."

            1 Reply Last reply
            0
            • B BobJanova

              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.

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups