我已经做了很多方法重载,但现在我有一个实例,我想重载一个属性。 Visual Studio 中的 IDE 似乎允许这样做,因为我实际上可以设置两个重载,但是我收到一个错误消息,说它无效,因为它们只是类型不同。我想我的语法中遗漏了什么?
我希望能够使用两个(或更多)不同的自定义类作为我的属性的类型。
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
到目前为止,我找到的所有帮助都与重载方法有关。尽管 IDE 允许我这样做,但我开始认为这是不可能的?
最佳答案
要重载某些东西——方法或属性——你需要它接受一组不同的参数。由于 VB.NET 中的属性可以接受参数,我猜您可以重载它们;但它们必须不同。
所以你可以这样做:
Public Overloads Readonly Property Average() As Double
Public Overloads Readonly Property Average(ByVal startIndex As Integer) As Double
但不是这个:
Public Overloads Readonly Property Average() As Double
Public Overloads Readonly Property Average() As Decimal
关于vb.net - 有没有办法在 .NET 中重载属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2238048/