[RESOLVED] Call method on inherited UserControls
I have a user control that I use for a base for a number of inherited controls:
VB Code:
Public Class JobDisplayBlock
Inherits System.Windows.Forms.UserControl
'
'
End Class
From this class, I inherit a number of different classes, as below. All the inherited classes have a method called Clear, which I call to reset text, numbers, etc within the class:
VB Code:
Public Class StationDisplay
Inherits JobDisplayBlock
Public Sub Clear()
'Code to zero counts and clear text, etc
End Sub
End Class
How can I loop through all these classes and call the Clear method on each one, just by checking if the type of object is the base class?
VB Code:
Public Shared Sub ClearJobLabels(ByRef ctlColl As Control.ControlCollection)
For Each ctl As Control In ctlColl
If TypeOf ctl Is JobDisplayBlock Then
'Call Clear method on all inherited classes
End If
If ctl.HasChildren Then
ClearJobLabels(ctl.Controls)
End If
Next ctl
End Sub
Hope that makes sense, thanks.
EDIT: I am using VB2003 standard.
Re: Call method on inherited UserControls
If JobDisplayBlock exists solely as a base for other classes then you should declare it MustInherit. You can then declare a Clear method as MustOverride and then override it in every derived class. That will allow you to call Clear on a JobDisplayBlock reference and invoke the derived class's implementation. If you will want to be able to create actual instances of the JobDisplayBlock class itself then you can't declare it MustInherit. In that case just declare a Clear method as Overridable and then override it in each derived class.
Re: Call method on inherited UserControls
The JobDisplayBlock class is used soley as a base class, so declaring it as MustInherit, and declaring the sub as MustOverride was the key.
It now works as I had hoped!
Many thanks for your help once again. :thumb:
Re: Call method on inherited UserControls
Don't forget to mark your thread as Resolved.