Setting a range value in a struct

hi i have the following struct

      public struct Camera
      {
            public Coordinate Coordinate;
            public int Pitch;
            public int Altitude;
            public int Direction;
            public int Distance;
      }

what i want to is when the value is set for Direction i want to only allow 1 to 360 positive range to be allowed, how would i do this within my sruct so that i do not have to include any validation code on all the pages which use the struct.

I hope this makes sence
chears
LVL 2
Robinsonx6Asked:
Who is Participating?
 
Razzie_Commented:
Hi,

Might want to rewrite it using Direction as a property:

struct Camera
{
      private int direction;

      public int Direction
      {
            set
            {
                  if(value < 0 || value > 360)
                        throw new ArgumentException("Value must be between 0 and 360");
                  this.direction = value;
            }
      }
}

HTH,

Razzie
0
 
Robinsonx6Author Commented:
sorry for delay as lost all internet due to crazy lorry driver crashing into telecoms pole outside office.

will i need to put any code on the pages which use the struct?
0
 
Razzie_Commented:
I'm not quite sure what you mean. You can just use this code for the struct and use it like you used the struct before. Is that what you want to know?

Hope the lorry driver is ok ;)

Razzie
0
 
Robinsonx6Author Commented:
lorry driver is ok but as he was pissed and put me 6 days behind deadline i would like to kill him, lol, it took so long to repasir as this is mauritius!!!!

yeah that is what i meant ill try it shortly.

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.