Inheritance Bugs in Windows Forms

I'm using the Visual Studio 2005 to do WinForms development and have been repeatedly running into problems with inheritance. What happens is that sometimes when I override methods in a base class the override isn't called - but the base class method is called instead. I've verified repeatedly that the code is correct, the method signatures match, and the override should be called (polymorphically) rather than the base class method - and yet it calls the base method instead. I haven't seen in in none Form-based classes so far, but I've seen it several times in specialized Forms that I've created. In each case, the problem is the same. The base class method is invoked rather than the override method. It appears to be a bug in the compiler. Note that these forms typically have several depths of specialization, and it may be that Microsoft hasn't tested them beyond a simple inheritance structure. It's a major problem for complex WinForms applications.


Answer this question

Inheritance Bugs in Windows Forms

  • moondaddy

    I don't think the compiler knows anything about Windows Forms that would make it behave differently, so frankly I would leave the compiler alone for the moment.

    If you could write a minimal repro code and post it, someone might try to help you find the issue.

    HTH
    --mc


  • Silver Haired Newby

    I created a test solution in Visual Studio 2005. I created 4 separate projects, with a 7-level inheritance hierarchy from System.Windows.Forms.Form spread among them. In the base form, I added a virtual and a non-virtual method. I also created a button which called both methods when clicked. Then I scattered throughout the rest of the forms various overriding and hiding methods; some called the base version, some did not. In the most-derived form, I added another button which also called both the virtual and the non-virtual method. I then ran a number of tests, commenting out various combinations of the methods in the inheritance chain. In all cases, the virtual and non-virtual calls were executed as they should be. Both buttons always resulted in the same call chain for the virtual methods; the non-virtual method call was different, as expected.

    I am afraid that without some sample code or a reproducable case, there is no way to determine the cause of your problem.



  • RPagels

    I'm sorry if you think it arrogant to assume that the problem is in the compiler, but it's the only explanation that seems to make sense. In re: your questions:

    1. Repo code - I'll see what I can put together when I have a chance. I can't post the literal code here because it's protected by a non-disclosure.

    2. Override - Yes. I'm using an override on the method and have confirmed that the signatures are exact between the base class and the override (other than the override keyword) by copying the base method signature into the inheriting class and changing the virtual keyword to override.

    3. Warnings - There are no warnings whatsoever during compile. And yes, I've used the "rebuild solution" choice to build the project.

    When you run the code, the base class method (the one marked virtual) is invoked, but the override method is never invoked. I've seen this problem in three separate areas already. Always in a class that inherits from System.Windows.Forms.Form . It appears to be related to the level of inheritance (3 or 4 levels above the .Net library classes) and may be related to the fact that the class hierarchy is distributed between projects (DLL projects included in the solution - NOT the DLL's as references, the projects themselves are included).

    Further, this is code that worked before I started compiling it in Visual Studio 2005. The problem only appeared after it was ported to VS 2005.



  • BertinL

    Not that Microsoft is perfect, but assuming that the problem is in the compiler, and that "Microsoft hasn't tested them beyond a simple inheritance structure", is remarkably arrogant. Visual Studio has many known bugs, due to its incredibly large feature set; but the number of bugs in the RTM C# compiler has been extremely low, and i highly doubt that yours is the most complicated program that has been compiled with it. Perhaps if you could post some repro code, someone could help you find the problem.

    To start with, are you using the overrides keyword Are you getting any warnings about member hiding when you compile



  • Inheritance Bugs in Windows Forms