Hi,
I've read the questions/answers explaining that there is no multiple inheritance in C#, that we don't even need it, and that it causes too much problems.
Now, I'm working on a project where I don't really understand how can I do things without multiple inheritance, without duplicating code.
Here's the situation. There is a website with a home page and other pages inheriting from a masterpage (the home page does not inherit from). Both the page and the masterpage are performing some stuff: custom login, statistics, loading of users settings for customization, etc. For the moment, the solution is crappy, since the source code for those tasks is just copied twice.
The home page class inherits from Page
. The masterpage, on the other hand, inherits from Masterpage
. Logically, it would be great to inherit from a common class too, but it's multiple inheritance, so it's impossible.
So what to do instead?
I thought about several ways, but dislike them:
Create a standalone class which will be called from the page/masterpage class. So for example instead of writing
bool isDisplayingTips = this.CurrentUser.IsDisplayingTips
, I would writebool isDisplayingTips = this.SharedObjects.CurrentUser.IsDisplayingTips
. I don't like it, since it's longer to write.Create a "real", empty, common masterpage, and inherit both the home page and the masterpage from it. Not only it will require to write more code to access masterpage parameters, but it will also slow the things down, requiring an additional masterpage on each request.
Any idea?