Results 1 to 3 of 3

Thread: Error Checking with the Set Statement

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    31

    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

  2. #2
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Cool 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.

  3. #3
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    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!
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width