I have a class that is derived from the System.Windows.Forms.Timer class and I would like to make Interval ReadOnly.
Is this possible?
Printable View
I have a class that is derived from the System.Windows.Forms.Timer class and I would like to make Interval ReadOnly.
Is this possible?
if you inherit the timer ( as a custom class )
declare Interval as a Shadows Property, like this ...
VB Code:
Public Class tmr Inherits Timer Public Shadows ReadOnly Property Interval() As Integer Get End Get End Property End Class
Because the Interval property is not declared Overridable, Shadowing is the only possibility. Just note though that while Shadowing is similar to Overriding, a Shadowed member is tied to the type, while an Overridden member is tied to the object. This means that if you have member A in a base class and you Override it in a derived class, casting a reference to a derived object as the base type will still only give you access to the derived implementation of member A. If you have member B in the same base class and you Shadow it in the same derived class, casting the same reference will actually give you access to the base implementation of member B. Try the following code to see what I mean:VB Code:
Public Class Class1 Public Overridable Sub MemberA() MessageBox.Show("Base implementation of MemberA.") End Sub Public Sub MemberB() MessageBox.Show("Base implementation of MemberB.") End Sub End Class Public Class Class2 Inherits Class1 Public Overrides Sub MemberA() MessageBox.Show("Derived implementation of MemberA.") End Sub Public Shadows Sub MemberB() MessageBox.Show("Derived implementation of MemberB.") End Sub End ClassAlso note that Overriding only affects the member with the same signature, i.e. a single overload if there are multiple, while Shadowing affects all members with that name. This doesn't mean that you should not use Shadows, but it is good to know what the implications are.VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myClass2 As New Class2 myClass2.MemberA() myClass2.MemberB() Dim myClass1 As Class1 = CType(myClass2, Class1) myClass1.MemberA() myClass1.MemberB() End Sub
Thank you guys very much