As I read here http://msdn.microsoft.com/en-us/library/75e8y5dd%28v=VS.100%29.aspx
It is possible to have get
in an Interface BUT NOT set
?
OR if I want getter and setter in Interface, do I have to use the old syntax getVar
setVar
just because new syntax doesn't fit Interface syntax?
Update: If I must omit set
in Interface, does this means I cannot enforce class to have setter which defeats the purpose of having an Interface in this case as I can only partially enforce?
Technically the class that implements the interface is free to use a property or a getter. In the same way there is no way for us to specify that a property is a setter in an interface, but we can still use a setter in our class.
You CAN use both get and set in interface property!
To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B . You can do this e.g. by passing it as a parameter to a method of B , or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter .
The getter function is used to retrieve the variable value and the setter function is used to set the variable value. Remember: You can directly access public member variables, but private member variables are not accessible. Therefore, we need getter functions.
No. I think you misunderstood. That article is about the possibility of having an interface with a readonly property (a property with only getter). But, if you need, you can put also the setter in the interface:
interface IHasProperty
{
string Property{ get;set; }
}
class HasProperty:IHasProperty
{
public string Property{ get;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