2

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:

  1. Why Image is the only property declaration in this class, but it must be called Overloads?
  2. The Property doesn't have any parameter (of course), so how could Overloads possible?

Thanks for reading.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181

2 Answers2

4

Because the Image property in the ButtonBase class isn’t declared as Overridable you cannot override it in derived classes.

You can shadow the parent declaration (i.e. hide it) by redeclaring it in the deriving class as Shadows or Overloads. The difference between these two is rather small (§1.15.3 in the VB language specification):

  • Shadows shadows by name: if a method (or property) is declared Shadows then it shadows all base class methods (or properties) of the same name.

  • Overloads shadows 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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • If then, why can we use `Overloads`? Isn't it only use for methods? And how could the language distinguish the overloaded properties, since they don't have any unique parameters? – Luke Vo May 27 '11 at 11:07
  • @W.N. You *can* overload properties in VB because properties in VB (not in C#!) can have arguments. Why it’s possible here, I don’t know. – Konrad Rudolph May 27 '11 at 11:09
  • @W.N. is a special "version" of a method that encapusulates a getter and or setter method. – Jodrell May 27 '11 at 11:10
  • @Konrad Rudolph: I see your edited answer. But I don't understand about `Shadows` very much, and it make my inherited properties not work properly, so I always use overrides for my UserControls. – Luke Vo May 27 '11 at 11:11
  • @Jodrell: maybe it's an acceptable answer, since we don't have a better understanding about it. – Luke Vo May 27 '11 at 11:11
  • @W.N. Yes, `Shadows` is rarely (if ever) useful. In fact, I’ve never used it. But unfortunately the property simply cannot be overridden. – Konrad Rudolph May 27 '11 at 11:14
  • @Konrad Rudolph In fact, using `Overloads` for `Image property` acts just like using `Overrides` for `Text property`, so it's not a really big problem. Just somehow, weird in concept. I only needed to know why this happens. – Luke Vo May 27 '11 at 11:19
  • 1
    @W.N. I’ve looked it up in the VB spec and have found out why `Overloads` works here. See updated answer. – Konrad Rudolph May 27 '11 at 11:31
  • See [Shadows vs Overloads in VB.NET](http://stackoverflow.com/questions/2515037/shadows-vs-overloads-in-vb-net/4760614#4760614) for confirmation of @KonradRudolph's answer. – Mark Hurd Jun 19 '11 at 10:43
1

Because there is no Overidable Image Property on UserControl. If you want to declare it with the same signature you would need to use the Shadows keyword, this could in turn be Overridable for the next inheritor.

EDIT:

Its an indication that the Author of button doesent think you should override image.

Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • Really? I don't think overriding Image property cause problem, since we can even override OnPaint event. – Luke Vo May 27 '11 at 11:13
  • @W.N. I'm not the author of `Button`. What is special about about your implementation of `Image`, why does the base `Image` not work? – Jodrell May 27 '11 at 11:16
  • lol, funny (but, yeah, you're right) answer. BTW, Thanks for your answer. +1 vote :) Happy coding. – Luke Vo May 27 '11 at 11:17
  • There're something else I need to do when user set data for Image property, so I have to overrides it. – Luke Vo May 27 '11 at 11:21