How to create mutually exclusive properties in C# -


i have class 3 properties:

class pricecondition {     public product salescode {...}     public controldate condition {...}     public pricedetail pricing {...} } 

any instance of pricecondition can have either salescode or condition. if condition chosen, pricing required, that's not pertinent discussion.

being relatively poor programmer, tried following:

    public product salescode     {         { return _salescode; }         set          {             this.condition = null;             _salescode = value;          }     }      public controldate condition     {         { return _cond; }         set          {             this.salescode = null;             _cond = value;          }     } 

in hindsight, it's obvious why created stack overflow. searching right way, found this answer xoring list, can't figure out how apply i'm trying do, since except ienumerable method, , i'm not using list<t> or similar.

how can ensure 1 of properties set @ time? can pass in var codeorcondition parameter in constructor, use typeof figure out is, assign appropriately? sort of understand said, having foreknowledge of whether that's going work or not helpful before set out write code.

update:

after excellent found in answer(s), ended this:

public class pricecondition {     #region constructor     /// <summary>create empty pricecondition.     /// </summary>     /// <remarks>an empty constructor required entityframework.</remarks>     public pricecondition();      /// <summary>create pricecondition uses sales code.     /// </summary>     /// <param name="salescode">the product use.</param>     public pricecondition(product salescode)     {         salescode = salescode;         condition = null;         pricing = null;     }      /// <summary>create pricecondition uses datecontrol , price (e.g. "0d7")     /// </summary>     /// <param name="datecontrol">the datecontrol condition use.</param>     /// <param name="price">the pricedetail use.</param>     public pricecondition(condition datecontrol, pricedetail price)     {         salescode = null;         condition = datecontrol;         pricing = price;     }     #endregion .... } 

second update:

entityframework got in way. knew required empty constructors, didn't realize depth of why. we're finding using private set keeping ef populating objects database. @ least that's looks like. we're persisting data in our dbinitializer, information isn't getting loaded pricecondition when try use it. easy answer seemed putting setters standard backing-store method , relying on business logic layer never set salescode , controldate on same pricecondition. that's not working either. we'll bang away @ more, suggestions appreciated.

set backing variables instead of properties:

public product salescode {     { return _salescode; }     set      {         _cond = null;         _salescode = value;      } }  public controldate condition {     { return _cond; }     set      {         _salescode = null;         _cond = value;      } } 

another approach prefer make immutable object, , provide constructors possible configurations:

class pricecondition {    public product salescode { get; private set; }   public controldate condition { get; private set; }   public pricedetail pricing { get; private set; }    public pricecondition(product salescode) {     salescode = salescode;     condition = null;     pricing = null;   }    public pricecondition(controldate condition, pricedetail pricing) {     salescode = null;     condition = condition;     pricing = pricing;   }  } 

(you should validate parameters in constructors also, shows principle.)


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -