On Sun, 19 May 2002 01:09:25 -0700, Jon Jagger <[EMAIL PROTECTED]> wrote:

>On Fri, 17 May 2002 19:00:49 +0100, Simon Robinson
><[EMAIL PROTECTED]> wrote:
>
>>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?
>
>Setting is different from initializing, so does this help?
>Cheers
>Jon Jagger
>

Or even better...


using System;

public struct AB
{
    public AB(int a, int b)
    {
        if (a >= b) {
            throw new Exception("...");
        }
        this.a = a;
        this.b = b;
    }

    public int A
    {
        get {
            return a;
        }
    }

    public int B
    {
        get {
            return b;
        }
    }

    private int a,b;
}

public class T
{
    public T(AB ab)
    {
        this.ab = ab;
    }

    public AB AB
    {
        get {
            return ab;
        }
        set {
            ab = value;
        }
    }

    public int A
    {
        get {
            return ab.A;
        }
        set {
            ab = new AB(value, ab.B);
        }
    }

    public int B
    {
        get {
            return ab.B;
        }
        set {
            ab = new AB(ab.A, value);
        }
    }

    private AB ab;
}

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