I have a class in vb.net like
Public Class Customer
Private _Name As String
Public Overridable Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
and a class deriving from it
Public Class ProxyCustomer
Inherits Customer
Private _name As String
Public Overrides WriteOnly Property Name() As String
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
which gives me following error
Public Overrides WriteOnly Property Name() As String’ cannot override ‘Public Overridable Property Name() As String’ because they differ by ‘ReadOnly’ or ‘WriteOnly’
but i have same construct in C#
public class Customer
{
public virtual string FirstName { get; set; }
}
public class CustomerProxy:Customer
{
public override string FirstName
{
set
{
base.FirstName = value;
}
}
}
which works, so the first thing is , is this consistent because 2 languages are behaving in a very inconsistent way.
secondly, when i do a reflection to get a property, so for example
Dim propInfo = GetType(Customer).GetProperty("Name")
the propINfo.canRead property is always false, shouldn’t this be true since base class implements the getter of the property ?
many thanks
I’ll deal with the 2nd part first. In your current vb.net code, the derived Name property replaces the original for the reflection lookup. So that reflection code only see the
WriteOnlyversion of the property. Worse, you completely replace the backing store in your derived setter, so your getter is checking a completely different variable than the one you set.As for the first question, when overriding properties in vb.net, you always need to override both the getter and the setter, even if you just replace it with an identical implementation:
The code should read like this: