Is there a way for implementers of an interface where a ReadOnly
property is defined to make it a complete Read/Write Property
?
Imagine I define an interface to provide a ReadOnly
Property
(i.e., just a getter for a given value) :
Interface SomeInterface
'the interface only say that implementers must provide a value for reading
ReadOnly Property PublicProperty As String
End Interface
This means implementers must commit to providing a value. But I would like a given implementer to also allow setting that value. In my head, this would mean providing the Property
's setter as part of the implementation, doing something like this :
Public Property PublicProperty As String Implements SomeInterface.PublicProperty
Get
Return _myProperty
End Get
Set(ByVal value As String)
_myProperty = value
End Set
End Property
but this will not compile as, for the VB compiler, the implementer no longer implements the interface (because it is no longer ReadOnly
).
Conceptually, this should work, because, in the end, it just means Implement the getter from the Interface, and add a setter method. For "normal methods", this would be no problem.
Is there some way of doing it, without resorting to "interface hiding" or "home-made" SetProperty()
method, and style having the Property
behave like a Read/Write property in the implementations ?
Thanks !
--UPDATE-- (I have moved this question to a separate Question) My question is really : "why can't this be done in VB.NET", when the following is valid in C#.NET?" :
interface IPublicProperty
{
string PublicProperty { get; }
}
with implementation :
public class Implementer:IPublicProperty
{
private string _publicProperty;
public string PublicProperty
{
get
{
return _publicProperty;
}
set
{
_publicProperty = value;
}
}
}
Interface with readonly property is assignable to interface with mutable property. interface MutableValue<T> { value: T; } interface ImmutableValue<T> { readonly value: T; } let i: ImmutableValue<string> = { value: "hi" }; i.
You just specify that there is a property with a getter and a setter, whatever they will do. In the class, you actually implement them. The shortest way to do this is using this { get; set; } syntax. The compiler will create a field and generate the getter and setter implementation for it.
Read-only fields in visual basic can be created by using ReadOnly keyword. In visual basic, the read-only fields can be initialized either at the declaration or in a constructor. The read-only field values will be evaluated during the run time in visual basic.
An interface can contain declarations of methods, properties, indexers, and events. However, it cannot contain fields, auto-implemented properties.
Now supported in Visual Studio 2015.
What's New for Visual Basic
Readonly Interface Properties
You can implement readonly interface properties using a readwrite property. The interface guarantees minimum functionality, and it does not stop an implementing class from allowing the property to be set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With