Multiple Inheritance C++ & C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Meyer

    Multiple Inheritance C++ & C#

    Hi all,

    I'm converting some C++ Controls to C# and there's one big thing, that
    I can't solve:

    class CControlEx
    {
    int nDescriptionID;
    CString strDescription;

    public:

    //works with nDescriptionID & strDescription
    void DoSomething();
    };

    with this class I create the following

    class CComboBox2 : public CComboBox, CControlEx
    {
    ...
    };

    class CEdit2 : public CEdit, CControlEx
    {
    ...
    };

    ....

    The advantage is that I have to code the logic for DoSomething in all
    of my derivations only once.

    In C# I use interfaces and it works fine, but is there a workaround,
    so that I don't have to code the same thing for DoSomething in all of
    my classes?
    Attributes and Reflection aren't a solution, because the values of
    nDescriptionID and strDescription may change at runtime.

    Thanks in advance
    Andy
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Multiple Inheritance C++ & C#

    Andy,

    As you have figured out, Multiple Inheritance (MI) is not supported in
    ..NET (it is supported in Eiffel, but it is not true MI, but rather, an
    obfuscation based on interfaces).

    To get around this, I would declare a base class of CControlEx (which
    should be named ControlEx, the naming guidelines for .NET indicate that you
    should not use C in front of class names to identify them as classes), and
    then derive CComboBox2 from CControlEx. CControlEx would declare
    nDescriptionId and strDescription, which would be marked as protected (or
    have protected properties wrapping the private member names). Then,
    DoSomething would access those members. Also, you might want to declare
    DoSomething as virtual, which would allow you to have a base implementation,
    but a custom one if needed in a derived class.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Andy Meyer" <am72de@yahoo.d e> wrote in message
    news:6e480b67.0 311120650.734ae f1@posting.goog le.com...[color=blue]
    > Hi all,
    >
    > I'm converting some C++ Controls to C# and there's one big thing, that
    > I can't solve:
    >
    > class CControlEx
    > {
    > int nDescriptionID;
    > CString strDescription;
    >
    > public:
    >
    > //works with nDescriptionID & strDescription
    > void DoSomething();
    > };
    >
    > with this class I create the following
    >
    > class CComboBox2 : public CComboBox, CControlEx
    > {
    > ...
    > };
    >
    > class CEdit2 : public CEdit, CControlEx
    > {
    > ...
    > };
    >
    > ...
    >
    > The advantage is that I have to code the logic for DoSomething in all
    > of my derivations only once.
    >
    > In C# I use interfaces and it works fine, but is there a workaround,
    > so that I don't have to code the same thing for DoSomething in all of
    > my classes?
    > Attributes and Reflection aren't a solution, because the values of
    > nDescriptionID and strDescription may change at runtime.
    >
    > Thanks in advance
    > Andy[/color]


    Comment

    • Rakesh Namineni[MSFT]

      #3
      RE: Multiple Inheritance C++ &amp; C#

      You could do something like Aggregation if that is an acceptable design in
      your context

      class CControlEx {}

      class CComboBox2 : CComboBox
      {

      // Your CComboBox2 will not implement "DoSomethin g". There
      are no interfaces either.
      // If you appropriately expose the contents of CControlEx
      then you can access them in this
      // CComboBox2 class

      private CControlEx myInnerControl;

      public CControlEx CControlEx
      { get { return myInnerControl; } }
      }

      Now clients of CComboBox2 will have to do the following...

      ClientOfCComboB ox2
      {
      ..........
      CComboBox2 comboBox;

      comboBox.CContr olEx.DoSomethin g(); // Your class did not
      implement DoSomething(). They forward it to the

      // contained Control.

      }

      --------------------[color=blue]
      >From: am72de@yahoo.de (Andy Meyer)
      >Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      >Subject: Multiple Inheritance C++ & C#
      >Date: 12 Nov 2003 06:50:23 -0800
      >Organization : http://groups.google.com
      >Lines: 41
      >Message-ID: <6e480b67.03111 20650.734aef1@p osting.google.c om>
      >NNTP-Posting-Host: 217.2.190.98
      >Content-Type: text/plain; charset=ISO-8859-1
      >Content-Transfer-Encoding: 8bit
      >X-Trace: posting.google. com 1068648623 7925 127.0.0.1 (12 Nov 2003[/color]
      14:50:23 GMT)[color=blue]
      >X-Complaints-To: groups-abuse@google.co m
      >NNTP-Posting-Date: Wed, 12 Nov 2003 14:50:23 +0000 (UTC)
      >Path:[/color]
      cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new s-out.cwix.com!ne wsfeed.cwix.co
      m!news.maxwell. syr.edu!postnew s1.google.com!n ot-for-mail[color=blue]
      >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1986 90
      >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
      >
      >Hi all,
      >
      >I'm converting some C++ Controls to C# and there's one big thing, that
      >I can't solve:
      >
      >class CControlEx
      >{
      > int nDescriptionID;
      > CString strDescription;
      >
      >public:
      >
      > //works with nDescriptionID & strDescription
      > void DoSomething();
      >};
      >
      >with this class I create the following
      >
      >class CComboBox2 : public CComboBox, CControlEx
      >{
      > ...
      >};
      >
      >class CEdit2 : public CEdit, CControlEx
      >{
      > ...
      >};
      >
      >...
      >
      >The advantage is that I have to code the logic for DoSomething in all
      >of my derivations only once.
      >
      >In C# I use interfaces and it works fine, but is there a workaround,
      >so that I don't have to code the same thing for DoSomething in all of
      >my classes?
      >Attributes and Reflection aren't a solution, because the values of
      >nDescription ID and strDescription may change at runtime.
      >
      >Thanks in advance
      >Andy
      >[/color]


      Rakesh, EFT.

      This posting is provided "AS IS" with no warranties, and confers no rights.
      Use of included script samples are subject to the terms specified at
      Use these online forms to report copyright and trademark infringement to Microsoft Legal. Infringement notices must comply with the Digital Millennium Copyright Act.


      Comment

      Working...