|
-
Mar 7th, 2005, 12:17 PM
#1
Thread Starter
Junior Member
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
-
Mar 7th, 2005, 02:19 PM
#2
Frenzied Member
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....
Magiaus
If I helped give me some points.
-
Mar 8th, 2005, 07:03 AM
#3
Fanatic Member
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|