Simon Robinson spake: > > Hi guys > > I have a class with two integer properties - call them A and B. > There is a condition that the value of B should always be greater than A, > otherwise the class won't function correctly. I'd like the error to be > detected and an exception thrown when client code sets the properties, > and I'm trying to figure out if there's any way of doing this that's > consistent with the normal .NET usage guidelines that it should be > acceptable to set > properties in any order. > > Any ideas? Is what I want to do possible? > > Simon
One way to do this would be to implement ISupportInitialize in your class (component). In the BeginInit method you would set a flag that would suppress validation. In the EndInit you would execute the validation code and throw any required exceptions. You might still have to suppress the validation in design time (using the DesignMode property of Component) because the designer might set your property in any order. I have not checked to see if BeginInit is called before the designer sets properties, but I would guess that it is not. This system would implement runtime validation, but you would still not have design time validation. You could implement design time validation by creating your own designer or editor. I have not done this yet so I can not provide any hints. I have implemented the ISupportInitialize interface along with suppressing validation when DesignTime is true, and it works adequately. -- Peter You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.