0

I have a simple problem: I have a struct Coordinates3D which is basically a 3D Point, but it also checks for boundaries. It has static maxX and minX and so on members, and checks the assigned coordinate value to fit the boundaries. It also has a boolean _check_boundaries, if this is false then it wont check boundaries and allow any values for coordinates. It looks basically like this

  struct Coordinate3D {
    public bool _check_boundaries;
    private int _x;
    public int X{
      get{ return _x;} 
      set { 
        if (_check_boundaries) 
        {  
          ... check min/max ...
        }
        _x = value;
      } 
    }
    ... y and x follow...
  }

I want to edit this object in propertyGrid (it is a property of a class). However, some classes have coordinates that are bounded, some not. So, for some classes, boundaries must be checked, but not for others. However, when i change this struct's property in propertyGrid, it seems to create a new struct each time. Therefore, it is either "check boundaries" for all cases or "do not check boundaries" for all cases, depending on what's set in the constructor.

Is there any way around this?

Istrebitel
  • 2,963
  • 6
  • 34
  • 49
  • 1
    are you sure you want to use a struct? http://stackoverflow.com/questions/521298/when-to-use-struct-in-c – Christopher Rathermel Mar 30 '12 at 17:48
  • 3
    You should probably make this a class and not a struct. Structs are passed by value rather than by reference and so each time you pass it to something else, a copy is made. – JamieSee Mar 30 '12 at 17:51
  • If i make it class its even worse. When i edit more than one objects in propertyGrid, it wont show them having the same x/y/z values (because classes differ by ref i think) and it will assign same class ref to all the objects i edit (so further changes to the coordinates in the code will affect ALL objects). Is there a way around it? – Istrebitel Mar 31 '12 at 06:10

0 Answers0