开发者

Either of the Properties to be assigned with a value

if there are two public properties in a class, and i will need either of them to be filled by who ever creates the object of this class; is there a way in C# that can impose this behavior?

So basically if Property1 has been g开发者_如何学运维iven a value, the user should not be able to give a value to Property2, or vice-verse?

If NOT, is there any best practice to do this, instead of creating 2 separate classes, with Property1 in one class, and Property2 in the second class?

Or may be a Method Attribute that can notify the user about this behavior? would that work?


You can put logic in the property setters to clear one property when the other is set.


you can add code in the setter. something kinda like this...

public class MyClass 
{
int one = -1;
int two = -2;

public int One { get { return this.one; }
                 set { if (this.two != -1 ) this.one == value; }}

public int Two { get { return this.two; }
                 set { if (this.one!= -1 ) this.two== value; }}
}


Just put the code to enforce your constraint into the setter of each property and you're done. Example:

using System;

public class MyClass {
    public static void Main()   { 
        TestClass tc = new TestClass();
        tc.Str1 = "Hello";
        tc.Str2 = "World!"; // will not be set because of enforced constraint
        Console.WriteLine(tc.Str1);
        Console.WriteLine(tc.Str2);
        Console.ReadKey();
    }
}

public class TestClass {
    private string _str1;
    public string Str1  {
        get { return _str1; }
        set {
            if (string.IsNullOrEmpty(Str2))
                _str1 = value;
        }
    }

    private string _str2;
    public string Str2  {
        get { return _str2; }
        set {
            if (string.IsNullOrEmpty(Str1))
                _str2 = value;
        }
    }
}

Output:

Hello

Notice that Str2's value is never set because Str1's is set first and therefore prints an empty string.


For two properties I would probably just do the setters check like in the other answers. But you could do something like this...

Instead of making it two properties, maybe they should be one. Contrived Example: Imagine instead of address having properties UsaZipCode (int) and CanadianPostalCode (string) it has one PostalCode like so:

class Address
{
    public string Street { get; set; }
    public IPostalCode PostalCode { get; set;}
}

public interface IPostalCode
{
    int? Usa { get; }
    string Canadian { get; }
}

public class UsaPostalCode
{
    private int _code;
    public UsaPostalCode(int code) { _code = code; }
    public int? Usa { get { return _code; }
    public string Canadian { get { return null; }
}

public class CanadianPostalCode
{
    private string _code;
    public CanadianPostalCode(string code) { _code = code; }
    public int? Usa { get { return null; }
    public string Canadian { get { return _code; }
}

Now address can never have both a USA and Canadian postal code. Is the extra complexity worth it? Depends on the use case.


public class MyClass
{
    private string pickedProperty = null;
    private object member1;
    private object member2;

    public object Property1 
    {
        get { return this.member1; }
        set 
        {
            if (this.pickedProperty == null)
                this.pickedProperty = "Property1";

            if (this.pickedProperty == "Property1")
                this.member1 = value;
        }
    }

    public object Property2 
    {
        get { return this.member2; }
        set
        {
            if (this.pickedProperty == null)
                this.pickedProperty = "Property2";

            if (this.pickedProperty == "Property2")
                this.member1 = value;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜