How does SET works with property in C#?

Posted by Richard77 on Stack Overflow See other posts from Stack Overflow or by Richard77
Published on 2010-05-07T12:22:19Z Indexed on 2010/05/07 12:28 UTC
Read the original article Hit count: 121

Filed under:

Hello,

I'd like to know how set works in a property when it does more than just setting the value of a private member variable. Let's say I've a private member in my class (private int myInt).

For instance, I can make sure that the the value returned is not negative

get
{
  if(myInt < 0)
    myInt = 0;
  return myInt;
}

With SET, all I can do is affecting the private variable like so

set { myInt = value; }

I didn't see in any book how I can do more than that. How about if I wan't to do some operation before affecting the value to myInt? Let's say: If the value is negative, affect 0 to myInt.

set
{
  //Check if the value is non-negative, otherwise affect the 0 to myInt
}

Thanks for helping

© Stack Overflow or respective owner

Related posts about c#