1

I am stuck in a doubt... I have an aspx page that has to inherit 2 classes:

1) Class expand : Page, IPostBackEventHandler 2) mydll.classX

But I am unable to accomplish the same. Any workaround that can be suggested would be really helpful...

Thanks

2
  • 1
    C# does not support multiple inheritence. Can you edit the DLL class?
    – Khan
    Sep 18, 2013 at 13:26
  • Hi Jeffery, thanks for your reply. No I cannot edit the dll class, thats where i'm really stuck.
    – cosmoloc
    Sep 18, 2013 at 13:39

2 Answers 2

3

You can't inherit from more than one class in .NET. You should look into one of these methods to solve your problem:

  • Make your classX inherit Page. Now your new class can inherit classX, and it'll be a child class of both clasX and Page;
  • Use interfaces instead of classes. You're kinda doing that already, with IPostBackEventHandler. You can't inherit from multiple classes but you can implement as many interfaces as you want;
  • Use composition - that is, your class has a member that is of type classX. Read about object oriented design patterns, that may help you accomplish what you want in elegant ways that make things easier. See this question: What is composition as it relates to object oriented design?
1
  • Thanks a lot for quick response.. My classX is a fixed dll class that I cannot change.. I will look into composition.. Thanks
    – cosmoloc
    Sep 18, 2013 at 13:33
1

Since you can't do multiple inheritance in what looks to be C# I think you will have to:

a. Replicate the public interface of myDll.classX and forward calls to an instance of myDll.classX

or

b. Make myDll.classX inherit Page, but that may not really be an option.

Without more information on what you are trying to accomplish, I can't really see any other options.

6
  • Hi Rusmus, Thanks for your reply. My ClassX is used to log in the user on an interface. This login interface has been coded in a masterpage. But my aspx page has some code related to aspx page too, including RaisePostBackEvent() which has to be within a class (Class expand), and for this function to work, i need to inherit this too. So when I inherit class expand, my login functionality stops working
    – cosmoloc
    Sep 18, 2013 at 13:37
  • Could you elaborate i bit more on the big picture of what you are trying to accomplish? I'm having a hard time understanding where you need to use the class Expand.
    – rusmus
    Sep 18, 2013 at 13:48
  • Hi Rusmus, My aspx page(TestPage.aspx) has 2 main parts : 1) Master page (which has a login, which does not works if my aspx page does not inherits mydll.ClassX) 2) TestPage's other code including IPostBackEventHandler events. It calls RaisePostBackEvent().So this function has to be inside a class (class expand which inherits System.Web.UI.Page). So My aspx page needs to inherit Class expand, to enable the postback events. So if I inherit Class expand, login functionality fails. While if I inherit the ClassX, then my C# code refuses to identify the gridview etc components on the web page
    – cosmoloc
    Sep 18, 2013 at 14:03
  • In that case you're pretty much stuck with option b from my answer, which is the same as Renans third option: use composition. TestPage.aspx needs to inherit from Page in order for the ASP.Net framework to work, so if you can't change ClassX, you will need to replicate the relevant parts of the interface and use an instance of ClassX internally.
    – rusmus
    Sep 18, 2013 at 14:04
  • Thanks a lot Rusmus, will try to implement it.
    – cosmoloc
    Sep 18, 2013 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.