问题
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?
回答1:
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
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
There is one way
Public Enum myType
inInteger = 0
inDouble = 1
inString = 2
End Enum
Public Class clsTest
Dim _Value1 As Integer
Dim _Value2 As Double
Dim _Value3 As String
Public Property MyValue(ByVal Typ As myType) As Object
Get
Select Case Typ
Case myType.inDouble
Return _Value2
Case myType.inInteger
Return _Value1
Case Else
Return _Value3
End Select
End Get
Set(ByVal value As Object)
Select Case Typ
Case myType.inDouble
_Value2 = value
Case myType.inInteger
_Value1 = value
Case Else
_Value3 = value
End Select
End Set
End Property
End Class
回答6:
It is not possible to overload properties. That being said, you could accomplish what you want by creating implicit conversions or overloading the =
operator.
回答7:
It would be possible to have the property operate on some special class, which supports widening conversion operators to and from the real types of interest. In some circumstances this could work reasonably well and provide a useful expansion. The biggest limitations:
- If the special class/struct gets converted to type Object, it won't behave like the thing to which it's supposed to be typecast.
- If the special thing is a class, then every time the property is get or set will require the instantiation of a new garbage-collected object.
Still, in some circumstances this may be a useful abstraction (especially if the base type would be a struct).
来源:https://stackoverflow.com/questions/2238048/is-there-a-way-to-overload-a-property-in-net