Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I have a small problem
 class a
{
   public void getmember();
    {
        ----
        -----
     }
}

class b
{
   public void  setmember();
    {
      ---
      ----
     }
}


class c:  a , b 
{
    public void display()
    {
---- 
 ---
    }
}



my problem is

class c: a , b not working

i.e it is allowing me to derive only from one class (a or b), but i want to derive from both classes (I don't want to change methods between classes)

please help me
Posted
Updated 18-Mar-11 1:00am
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-11 0:32am    
RTFM before asking. Why wasting so much time?
--SA

In C# and therefore in .net you can only inherit from one base class. You can achieve a kind of multiple inheritance by using interfaces:

Simulated Multiple Inheritance Pattern for C#[^]
 
Share this answer
 
Comments
Sridhar Patnayak 18-Mar-11 7:01am    
Jim
good link, I got solution
Thanks
Tarun.K.S 18-Mar-11 7:01am    
Exactly! It was possible in C++ though. 5d
Olivier Levrey 18-Mar-11 7:30am    
Good answer and good link. Have my 5.
Eduard Keilholz 18-Mar-11 8:08am    
Good call, my 5!
Sergey Alexandrovich Kryukov 19-Mar-11 0:31am    
This is called "weak form of multiple inheritance" and is a very well thought compromise.
--SA
Agree with jim.
.Net does not support multiple inheritance, but you can achieve it in help of Interfaces, and other way to do is multilevel inheritance, if that is what matches your requirement.
Multilevel inheritance limits the implementation and it may not comply many complex case scenarios, and using interfaces you can decide what to implement in deriving class.
plz see your code modified as possible solution.

class a
{
public void getmember();
{
...
}
}

class b: a
{
public void setmember();
{
...
}
}


class c: b
{
public void display()
{
...
}
}
 
Share this answer
 
v2
Comments
Tarun.K.S 18-Mar-11 7:02am    
This is right too! 5d
Olivier Levrey 18-Mar-11 7:31am    
Correct. It depends on what Op wants to do with his classes. My 5.
Sridhar Patnayak 18-Mar-11 7:43am    
Dear friends
I don't want solution like this, what jim provided link is useful
Sergey Alexandrovich Kryukov 19-Mar-11 0:31am    
See my comment to Jim's Answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900