This one took weeks to resolve



While working on a project, I decided that a certain collection of forms
would be better designed as deriving from a base class form. I created a new
form and then moved common code from the collection of forms along with a few
of the common controls. I then inherited the original forms from the new
base class. These derived forms supported events and to cloud the issue a
bit, each had static members for required timers and the base class contained
a thread member. I built the project and everything worked as expected. OK
this is done. I then went to work on another section of the project and
when doing build a day or two later I started getting some obviously
nonsensical complier error messages concerning one or more of the derived
classes discussed above, this without even having the files open. The exact
errors changed from time to time but common ones would insist that a control
didn't exist when the error was pointing to the very line that created the
control in the Windows generated code. Another favorite was insisting that
InitializeComponent couldn't be accessed because it is private. By fiddling
around I discovered that if I cut any line out of the file and pasted it back
in exactly as before, the errrors when away and I was ok again for a few
builds. I should also point out that the forms always worked correctly.
Eventually it was getting to be enough of a time waster that I needed to
resolve the problem. These derived classes where the only place in a project
of well over 100 forms where the odd behavior occured. I couldn't see
offhand what was different about these particular derived classes so I
started commenting out code and seeing if things started working. In the
meantime, I had noticed that a surefire way to trigger the errors was to add
a new unrelated form to the project. Add the form, do a build and bingo,
errors. To make it even more weird, if I just deleted the new form, the
errors when away.

Enough of the background. The answer is that when I created the base class,
I moved some controls from the soon to be derived classes but in one case, I
left the event handlers in the derived classes.

Class MyBase
Inherites Mycommonbase

' this class has a btn control
End Class

Class derived
Inherites MyBase

' this is what causes the errors.
' It is tricky because it works correcly and among all the many errors
' reported by the compiler, this was never mentioned

Private sub Btn_Click(...) Handles btn.click
....
end sub

End Class

What I should have done was

Class MyBase
...

Protected Overridable Sub Btn_Click(.........) Handles ...
End Sub

end class

Class Myderived ..

Protected Overrides Sub Btn_Click( ......)

End Sub

End Class

I probably would not have made this error if I had first created the base
class and then the derived class in the usual manner but because I did it the
opposite order, I just left the btn event handlers as they were which turned
out not to be a great idea.
--
JB
.



Relevant Pages

  • event-handling subs running twice in derived class.
    ... I created a base class that has a datagrid. ... possible so that any derived classes pass some info to the base ... Public Overrides Sub ToolBar1_ButtonClick(ByVal sender As ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Not pure virtual functions and mixing virtual and non virtual methods
    ... Should I call base class implementation or not? ... But doing the above assumes that one answer is good for all, that all derived classes will want the base class method called in the same way. ... More generally, the way I see it, if you combine invariant implementation with variant parts, you have no elegant way to change the variant part. ... Following them will remove all inheritance relationships. ...
    (comp.object)
  • Re: Why must I paint the form background for an owner-draw control?
    ... His problem stems from the fact that the button base Class has ... This problem does not appear in a class based upon Control ... that is why there is a black background. ... >> protected override void OnMouseDown ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: How to prevent base class (UserControl) form being serialized
    ... the base class so your control won't have a name, know its visibility, know ... My object happens to be derived from a control so I am penalizes for that. ... them on the code for the UserControl. ... Just serialize this structure ...
    (microsoft.public.dotnet.general)
  • Re: Controling Event Sequencing...
    ... need the OnPostClick event fire after the OnClick event is completely ... event queue and have it execute AFTER the OnClick in completely done ... ... control exposes this event to the programmer to allow him / her to ... So, what I want to be able to do, is create a base class object for this ...
    (microsoft.public.dotnet.languages.vb)