Of course, it seemed too easy!

The setter method does seem to be the best solution.

Seang
-----Original Message-----
From: Simon Robinson [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 17, 2002 11:49 AM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Setting properties


Hi Sean

Intriguing idea but trouble is that might fail if the properties had
been previously set and the client code wanted to change
both their values.
eg. current values are 0 and 50.
Client wants to change this to  100 and 200
-> will get an exception unless changes top value first.

Looking at the various replies to this I'm increasingly thinking that
there is no sensible way to consistently define the two values
as properties. You either have to not perform validation when you
set them, or you have to do something that compromises
the ability of client code to set them separately, either by
making them readonly and having a set method, as a couple of
people have suggested,  or having an immutable class, which
will have the same impat on client code.

Simon

---------------------------------------------------------------
Simon Robinson
http://www.SimonRobinson.com
---------------------------------------------------------------
----- Original Message -----
From: "Sean Greer (SBI-Chico)" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, May 17, 2002 7:38 PM
Subject: Re: [DOTNET] Setting properties


> I know it's cumbersome, but what about this:
>
> public class ALessThanB
> {
>         private bool    m_bASet = false, m_bBSet = false;
>         private int             m_nA = 0, m_nB = 0;
>
>         public int A {
>                 set {
>                         if(m_bBSet && value >= m_nB)
>                                 throw new ArgumentException("A must be
less
> than B.");
>                         m_nA = value;
>                         m_bASet = true;
>                 }
>         }
>         public int B {
>                 set {
>                         if(m_bASet && value <= m_nA)
>                                 throw new ArgumentException("B must be
> greater than A.");
>                         m_nB = value;
>                         m_bBSet = true;
>                 }
>         }
> }
>
> -----Original Message-----
> From: Simon Robinson [mailto:[EMAIL PROTECTED]]
> Sent: Friday, May 17, 2002 11:01 AM
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] Setting properties
>
>
> 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
>
> ---------------------------------------------------------------
> Simon Robinson
> http://www.SimonRobinson.com
> ---------------------------------------------------------------
>
> You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>
> You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to