First, please look at this custom Button-inherited UserControl code:
Public Class UserControl1
Dim _Text As String
Dim _Image As Image
<Browsable(True), Description("Gets or sets the text displayed on the button")> _
Overrides Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
_Text = value
MyBase.Text = value
End Set
End Property
<Browsable(True), Description("Gets or sets the image displayed on the button")> _
Overloads Property Image() As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
'ReDrawMe()
End Set
End Property
End Class
That’s ALL the code of the UserControl. The Overrides at Text property is OK, but I don’t know why VS tell me I CAN’T use Overrides at Image property, but I can use Overloads. Why? I thought Overloads only use if there’re multiple methods with the same name (different parameters). Two things I still doubt:
- Why
Imageis the only property declaration in this class, but it must be calledOverloads? - The
Propertydoesn’t have any parameter (of course), so how couldOverloadspossible?
Thanks for reading.
Because the
Imageproperty in theButtonBaseclass isn’t declared asOverridableyou cannot override it in derived classes.You can shadow the parent declaration (i.e. hide it) by redeclaring it in the deriving class as
ShadowsorOverloads. The difference between these two is rather small (§1.15.3 in the VB language specification):Shadowsshadows by name: if a method (or property) is declaredShadowsthen it shadows all base class methods (or properties) of the same name.Overloadsshadows by name and signature: it only hides a method of the same name and same signature.In your case, both result in the same because there is only a single property of that name.
Either way, if the parent property isn’t marked as overridable, then redefining it in a derived class is a bad idea – it won’t work properly when your control is accessed via its base class type.