views:

222

answers:

5

I've done plenty of Method Overloading, but now I have an instance where I would like to Overload a Property. The IDE in Visual Studio seems to allow it, since I can actually set up the two overloads, but I get an error saying it is not valid because they only differ in type. I think I'm missing something in my syntax?

I want to be able to use two (or more) different custom classes as the Type for my property.

Public Overloads Property myFlexibleProperty() As myCustomClass1
      Get
         Return _myFlexibleProperty1
      End Get
      Set(ByVal value As myCustomClass1)
         _myFlexibleProperty1 = value
      End Set
   End Property

   Public Overloads Property myFlexibleProperty() As myCustomClass2
      Get
         Return _myFlexibleProperty2
      End Get
      Set(ByVal value As myCustomClass2)
         _myFlexibleProperty2 = value
      End Set
   End Property

All of the help I have found so far has been concerning Overloading Methods. Despite what the IDE is letting me do, I'm beginning to think this is not possible?

+8  A: 

To overload something--method or property--you need for it to accept a different set of parameters. Since properties in VB.NET can accept parameters, I guess you can overload them; but they have to be different.

So you could do this:

Public Overloads Readonly Property Average() As Double
Public Overloads Readonly Property Average(ByVal startIndex As Integer) As Double

But not this:

Public Overloads Readonly Property Average() As Double
Public Overloads Readonly Property Average() As Decimal
Dan Tao
As Dan says--"yes, but the signatures must differ". The same rule applies to methods--so your sample would not work for methods either.
STW
@GSTD: Give them different names.
Dan Tao
@GSTD change either the method name, or the parameters (parameter names don't matter; parameter types are what matter). So MyMethod(object) differs from MyMethod2(object), and MyMethod(int, string) differs from MyMethod(string, int)
STW
Thanks Dan, that worked great! I was under the false assumption that Properties could NOT accept parameters
GSTD
@GSTD: As far as I know, in C# they can't. VB allows it. In general, though, I would recommend shying away from supplying properties with parameters unless you have a really good reason for it. Often I find that developers fall in love with properties and start using them like methods.
Dan Tao
+1  A: 

Your signatures are the same (only the return types differ). the compiler will not know which method you're calling. That is your problem. Change the signatures.

FiveTools
+2  A: 

This should not be possible. You are effectively trying to make a property that could return two different types. There is no way for the system to make the determination as to what you are trying to call.

You will have to give unique property names to each.

Mitchel Sellers
A: 

It is not possible to overload properties. That being said, you could accomplish what you want by creating implicit conversions or overloading the = operator.

NickLarsen
+1  A: 

have you tried using a class based on an interface? Then, you could have different classes based on the same common interface and the property associated to the interface type, not the specific class itself.

DRapp