tags:

views:

235

answers:

7

Hi.

I have a few questions related to the design of my User class but they are different enough that I think they should be independent questions.

So, the first is related to inheritance of base classes. I am currently inheriting two classes, ProfileBase and ISessionMgrEntry as such:

public class User : ProfileBase, ISessionMgrEntry

But, I also want to inherit a third class, MembershipUser, like this:

public class User : ProfileBase, MembershipUser, ISessionMgrEntry

However, the compiler won't let me do that. Why? And, how do I get around this?

Thanks.

PS - ASP.NET 3.5 / C#

EDIT

Hi. I think the below solution may work for what I am trying to acheive. It seems pretty simple and straight forward. I am doing this so I can create a complete/combined User object. Does anybody see any reason why this might cause problems? One that came up while I was ding this is overlapping properties. For example, both MembershipUser and ProfileBase share "UserName". Should I just chose one or the other or will this be a design flaw? Suggestions? Thanks again.

 public class User
    {

        #region Constructors
            private readonly MembershipUser _MembershipUser;
            private readonly ProfileBase _ProfileBase;
        #endregion

        public User()
        {
            _MembershipUser = new MembershipUser();
            _ProfileBase = new ProfileBase();

        }

        public string Comment
        {
            get { return _MembershipUser.Comment as string; }
            set { _MembershipUser.Comment = value; }
        }

        public bool IsAnonymous
        {
            get { return _ProfileBase.IsAnonymous as bool; }
        } 

        ....

   }
+13  A: 

In the first example you're not actually inheriting from two classes, but from one class and an interface.

C# doesn't allow multiple inheritance from classes, but does allow you to implement multiple interfaces. See this MSDN blog post for more information on why.

You will have to make an IMembershipUser interface and implement that in your User class.

Interfaces are usually given names based on the concrete class name prefixed by an I. So the class MembershipUser would have an interface IMembershipUser. There's nothing stopping you using some other name, but everyone who uses interfaces is used to this naming convention.

ChrisF
Thanks Chris. Could you take a look at my Edit when you have a moment? thanks again for your help.
Code Sherpa
@Code Sherpa - I'd really need to see more of your code and have more of a conversation than is possible via SO, but if you have got duplicated properties you ought to be looking at a different class hierarchy.
ChrisF
+1  A: 

C# doesn't support multiple inheritance.

In the first example you are inheriting from ProfileBase and declaring your class to be implementing the ISessionMgrEntry interface.

You can add as many interfaces to a class as you want (provided you implement them all).

Oded
+2  A: 

C# only supports single inheritance. You can either chain together your classes (i.e. MembershipUser inherits from ProfileBase) or use interfaces.

Jamie Ide
+4  A: 

hi,

in c# you just can inherit from one class, but implement as many interfaces as you want. in your case ProfileBase and MembershipUser are classes and ISessionMgrEntry is an interface.

nWorx
+1  A: 

C# does not allow multiple inheritance.

BlueRaja - Danny Pflughoeft
+1  A: 

C# only allows single inheritance: you can only inherit from one base class. In the first case ISessionMgrEntry is an interface which is different from a base class. Here's a good article on the difference: http://msdn.microsoft.com/en-us/library/ms973861.aspx

In the second case you are trying to inherit from both ProfileBase and MembershipUser, which the compiler won't allow. The best way to get around this would be to encapsulate one (or both) of ProfileBase and MembershipUser.

Jeff Hornby
A: 

Multiple interfaces with identical member names are not a problem. If the members can share the same implementation, just create a public member with the same name; if they can't share the same implementation, you can just explicitly implement one (or all) of the members.

For example, IEnumerable<T> inherits IEnumerable, and both provide a GetEnumerator() method with differing return types. Consequently, IEnumerable.GetEnumerator() is generally explicitly implemented:

class Foo : IEnumerable<object>{
    // explicitly implement IEnumerable.GetEnumerator()
    IEnumerator IEnumerable.GetEnumerator ()
    {
        return GetEnumerator();
    }

    public IEnumerator<object> GetEnumerator ()
    {
        yield break;
    }
}

Calling GetEnumerator() in IEnumerable.GetEnumerator() isn't an infinitely recursive call; it calls the public member of the same name, not itself (i.e. the IEnumerable.GetEnumerator() method).

jonp
thanks man - that sorted it out for me. much appreciated!
Code Sherpa