locked
How to create property with multiple default values ? RRS feed

  • Question

  • like :

    public string myString = ['a','b']

    A user  then can only change the property  to A or B.

    Cant find it...

    Henk.


    • Edited by H.P.Mulder Wednesday, September 7, 2016 2:09 PM
    Wednesday, September 7, 2016 2:06 PM

Answers

  • The BackgroundImageLayout property that you ask about is, in fact, an emum (like the one in Konrad's example).

    From your latest post it actually sounds a bit like you want two properties. One, called for example, CustomFieldOrigin, would be an enum of type CustomFieldType. This would be defined something like:

    public Enum CustomFieldType
    {
        Production,
        Custom
    }

    The other property CustomField could then be a string which would return one of either two other internal values (productiontime or customtime) based on the property value of CustomFieldOrigin.

    (It also sounds like "Elapsed production time" should be held as a Timespan or DateTime rather than a String as well, which could just format into a string when you display or output it, but that is possibly a different question).




    Friday, September 9, 2016 11:51 AM

All replies

  • Hi,

    you could create your own enumeration:

    public Enum MyAAndBEnum
    {
        A,
        B
    }

    and then use this enumeration for your property:

    public MyAAndBEnum MyEnum { get; set; }

    With kind regards,

    Konrad

    Wednesday, September 7, 2016 4:40 PM
  • Konrad is correct, although Enums are not strings.

    Another possible approach is to use properties (your example provides a field, not a property).  The general approach is:

    private string myString_;
    
    public string MyString {
        get { return myString_; }
        set {
            if (value == "a" || value == "b") 
                myString_ = value;
            else 
                throw new ArgumentOutOfRangeException();
        }
    }

    The enum version is better if you don't need a string, and when you know at compile time the complete set of values your variable should take on.

    The property version is better if the range may not be known until runtime, or may take on a very large range of values (for example a double property might be able to range from -1.0 to 7.0, the setter logic in a property could enforce that).

    Wednesday, September 7, 2016 4:57 PM
  • Hi,

    that is also a great idea. We could even mix the two possibilities:

    Just store the value internaly as enum. And the public property will parse the enum and return the enum value as string (ToString() call).

    With kind regards,

    Konrad

    Wednesday, September 7, 2016 5:22 PM
  • Personall I would avoid using Enums when working with strings. It can get complicated. It is possible, but then you need to parse enum to string by using some tryParse method, to make sure its really a string. Personally I would something similar like:

        class Program
        {
            static void Main(string[] args)
            {
                string a1 = "a";
                string b1 = "b";
                Letters l = new Letters(a1, b1);
    
                string newA1 = l.A;
                string newB1 = l.B;
            }
       }
    
       class Letters
       {
           public string A { get; private set; }
           public string B { get; private set; }
           public Letters(string a, string b)
           {
               this.A = a;
               this.B = b;
           }
       }


    Mitja


    • Edited by Mitja Bonca Wednesday, September 7, 2016 5:32 PM
    Wednesday, September 7, 2016 5:24 PM
  • Sorry, but you are replying as usual:

    - Where did I write that enum is a string type?

    - Where did I write, that it is not possible to assign any number?

    - Where is the request, that it must be a string? He talks about A or B - not "A" or "B". And you could do a lot with an enum type - but that is not the topic here.

    And my last question is: Where do you try to help the person who created the thread? All you do is insulting others. I do not care why you think that you have to insult me. You cannot behave and you show no respect. 

    With kind regards,

    Konrad

    Wednesday, September 7, 2016 6:16 PM
  • Thanks for all the suggestions,

    I realized I have to rethink my options.

    Case: I have created a control which shows production data, I wanted to make the "Elapsed production time" field on this control configurable by using a property.

    and Yes it would be a string,

    property Customfield where we  can choose [productiontime,customtime].

    Since I have only two options I thought it would be better to use Boolean for this:

     private Boolean customTime;
            public Boolean CustomTime
            {
                get { return customTime; }
                set
                {
                    customTime = value;
                }
            }

    But stiil my question remains,

    how to create a property like (See picture below) ?


    Friday, September 9, 2016 7:53 AM
  • The BackgroundImageLayout property that you ask about is, in fact, an emum (like the one in Konrad's example).

    From your latest post it actually sounds a bit like you want two properties. One, called for example, CustomFieldOrigin, would be an enum of type CustomFieldType. This would be defined something like:

    public Enum CustomFieldType
    {
        Production,
        Custom
    }

    The other property CustomField could then be a string which would return one of either two other internal values (productiontime or customtime) based on the property value of CustomFieldOrigin.

    (It also sounds like "Elapsed production time" should be held as a Timespan or DateTime rather than a String as well, which could just format into a string when you display or output it, but that is possibly a different question).




    Friday, September 9, 2016 11:51 AM