7 Replies - 16150 Views - Last Post: 19 September 2007 - 01:32 PM Rate Topic: -----

#1 tody4me   User is offline

  • Banned
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

C# Inheritance Syntax

Posted 11 September 2007 - 11:22 AM

more code cleanup help.
I have some code written within classes that I have "stacked" on top of each other, and I want to separate and use each class independently now. Examples would be Inventory, Orders, Customers, Files, etc, where I would have an Order File and within the order class would be another file handling class, an invoice class, details, etc. These are independent classes but also rely on one another for information. What i'm looking for, and have so far been unsuccessful in finding is the syntax to inherit the classes that are relied upon.

Such as
class Orders : Invoices, Files, Details
{
}


but this code gives an error - unable to inherit from more than one class.

Is the above the correct syntax and I'm doing something wrong or is there another syntax for this?

Is This A Good Question/Topic? 0
  • +

Replies To: C# Inheritance Syntax

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# Inheritance Syntax

Posted 11 September 2007 - 11:31 AM

Unfortunately, C# doesn't support Multiple Inheritance so you cannot do that, it wont allow it. What you can do however, is turn your multiple classes into Interfaces and C# supports Multiple Implementation, meaning you can inherit from more than one Interface.
Was This Post Helpful? 0
  • +
  • -

#3 tody4me   User is offline

  • Banned
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: C# Inheritance Syntax

Posted 11 September 2007 - 01:11 PM

Quote

Unfortunately, C# doesn't support Multiple Inheritance

yeah found out about the multiple inheritance the hard way, guess i just got use to doing it with multiples in C++ back in the day. Haven't really written anything in c++ in years though.

Quote

What you can do however, is turn your multiple classes into Interfaces and C#

yeah, i know (interfaces), but i'm not wanting to have to re-create the wheel every time that i want to use the class. I do want to have the ability to override the functionality of a given function, and that's why i wanted to make them all classes.
So the best way to do it then is the same as what i have now, where the classes are stacked on top of each other, so to speak?

Thanks again psycho
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# Inheritance Syntax

Posted 11 September 2007 - 01:21 PM

You dont have to recreate the wheel, the classes you have (the ones you wanted a class to inherit all of them) convert them to Interfaces, then you can inherit your classes from as many of them as you want.

Thats what I did, I had 4 classes that I was always inheriting from, I converted the 4 to Interfaces then created a single class that inherited all 4, thus removing about 5 or 6 class files.
Was This Post Helpful? 0
  • +
  • -

#5 tody4me   User is offline

  • Banned
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: C# Inheritance Syntax

Posted 11 September 2007 - 01:29 PM

I'm sorry if it seems like I'm slow with this, but my understanding of interfaces, probably from writing in C++ first, is that an interface is a "shell" of a class and does not have any functionality. maybe it's been redefined in C#, but that's how I understood interfaces back when i first learned about them.

I'll try it that way and see if I can get it to function the way I'm expecting. Thanks for the information again psycho.
Was This Post Helpful? 0
  • +
  • -

#6 jp42   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-August 07

Re: C# Inheritance Syntax

Posted 14 September 2007 - 01:48 PM

psycho is right, Program to an interface not an implementation...though that statement is more broad than just an actual 'interface'
Was This Post Helpful? 0
  • +
  • -

#7 gogole   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: C# Inheritance Syntax

Posted 15 September 2007 - 01:43 PM

the thing about replacing classes with interfaces for the purpose of multiple inheritence works as psycocoder described it (it is actually called aggregation in c#) .to connect everything you'll use fields in the derived class to access the interfaces. stacking up the classes could also work since classes deriving sequencially from each other is possible but there is a drawback:a class might derive methods and fields it won't use.
Inventory : Files {
........
}
Orders:Inventory{
........
}
 Customers: Orders{
.........
 }
Files:customers{
.........
}


Was This Post Helpful? 0
  • +
  • -

#8 gogole   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: C# Inheritance Syntax

Posted 19 September 2007 - 01:32 PM

made a mistake there, actually aggregation is also another way to work around multiple inheritance but it is not as i described it. a field is used to access class members of another class providing access to the class thus serving as psuedo multi inheritance,the drawback to this approach is that there is code duplication becuase new members added to the class will not be implemented in the field unless implementation isrepeated in the accessing field . the interface approach works just fine and the circular inheritance pattern i proposed according to my thoery should work, don't actually know if it will.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1