473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Inheritance - help please?

Hi,
I'm new to C# but i know a little of C++. My problem is inheritance within
c#, im not sure how to do it. For example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?

Thank you in advance,

Timothy.
Nov 16 '05 #1
4 1139
Timothy V <tr****@msn.com> wrote:
I'm new to C# but i know a little of C++. My problem is inheritance within
c#, im not sure how to do it. For example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?


You can't - as you've said, you can only inherit from one base class.
You can, however, implement multiple interfaces, if that helps. You
might like to consider using aggregation instead - have the HybridUser
class have references to Admin, Instructor and Student instances, and
have properties which allow access those instances.

Without more detail of what you're really trying to do, it's hard to
know exactly what the best solution is.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Timothy V" wrote...
I'm new to C# but i know a little of C++. My problem is
inheritance within c#, im not sure how to do it. For
example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?


There's several solutions to this, one is through aggregation:

interface IUser {}
interface IAdmin : IUser{}
interface IStudent : IUser{}
class Admin : IUser, IAdmin {}
class Student : IUser, IStudent {}
class HybridUser : IUser, IAdmin, IStudent
{
Admin a;
Student s;

// Method calls passed to the
// private instances
}

Another is to create roles for the user:

abstract class Role {}
class Admin : Role {}
class Student : Role {}

class User
{
ArrayList roles; // which contains the users roles
}

There are several others, but which one to choose is of course depending on
how the classes actually will fit into and will be used in your application.

// Bjorn A
Nov 16 '05 #3
"Timothy V" <tr****@msn.com> a écrit dans le message de
news:uV****************@TK2MSFTNGP12.phx.gbl...
Hi,
I'm new to C# but i know a little of C++. My problem is inheritance within
c#, im not sure how to do it. For example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?

Thank you in advance,

Timothy.


Hi Timothy

you are definitely thinking of multiple inheritance ! That is not possible
in C#.
However you can inherite from multiple interfaces.
Could you give some more details about the methods of each class ?

Fred
Nov 16 '05 #4
In your example Admin, Instructor and Stuent could all override methods of
the User base class - which of these overrides should be chosen for the
HybridUser class? (Note that this does not apply to interfaces, as they
don't contain an implementation)
That's the one of the major problems of multiple inheritance, and one of the
causes why it isn't possible in C#.
Personal suggestion: Build a class hierarchy for User-Groups:
UserGroup
Admin : UserGroup
Instructor : UserGroup
Student : UserGroup
And let each User object contain a list of UserGroups it belongs to.
That way you can enumerate what groups a user belongs to (not possible in
C++ without reflection), and decide for each method what should be done if
more than on UserGroup is in the list.

Niki

"Timothy V" <tr****@msn.com> wrote in
news:uV****************@TK2MSFTNGP12.phx.gbl...
Hi,
I'm new to C# but i know a little of C++. My problem is inheritance within
c#, im not sure how to do it. For example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?

Thank you in advance,

Timothy.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Indraseena | last post by:
Hi friends, Can anybody answer me where exactly is the private inheritance is used? example: class Base { public :
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.