Is it possible to ignore set and get when I’m assigning to or retrieving a value?
In specific, I’m inheriting from a class that has a property declared like this:
virtual public Int32 Value { get; set; }
What I’d like to do is to override it and do something useful in those set and get’s. The problem appears when I override it, I also have to manually assign, or return the value from the property. If I do something like this:
override public Int32 Value
{
get
{
return this.Value;
}
set
{
this.Value = value;
// do something useful
}
Then I’m creating an infinite loop. Is there a way to set or get the value without invoking the code in set and get, or do I have to make a separate name for the actual variable?
Instead of using
this.Value, you should be usingbase.Value. That will retrieve/set the property in the base class.Note that the base method actually has to be overridable (
virtualorabstract); in your example it’s not. If the base method is not virtual then you’ll just get a compiler error when you try to override in the derived class.