Error Checking with the Set Statement
Code:
private int Orientation
{
get { return Orientation; }
set
{
if ((value > 0) && (value < 5))
Orientation = value;
}
}
What I want to do is, if the variable is set to something that's not between 1 and 4, don't change the value of Orientation; however, when you try it, the "Orientation = value;" line triggers the "set" of the property again so I get a stack overflow. Any ideas? I'd like for the error checking to occur within the set statement if at all possible.
Dan
Re: Error Checking with the Set Statement
how about a private variable _orientation...?
Code:
private int _orientation = 0;
public int Orientation
{
get { return _orientation; }
set
{
if ((value > 0) && (value < 5) && (value != _orientation ))
_orientation = value;
}
}
Why do you have a private property anyway? a private variable is less code.... and it is bad practice to get or set a property value to/from it's self. Where is this value being stored. I would assume that it never works....
Re: Error Checking with the Set Statement
got to agree with Magiaus there. Surely your approach would just get stuck in a loop as it keeps trying to assign itself!