How do I avoid an infinite loop when overriding a virtual property?

Posted by cbsch on Stack Overflow See other posts from Stack Overflow or by cbsch
Published on 2010-03-21T02:49:11Z Indexed on 2010/03/21 3:21 UTC
Read the original article Hit count: 389

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#