-2

Data incoming from a SQL Server has a field with a pipe delimited string IE:

20190819|20190830|20190915

I would like my Class property to return a string[]. I can manually do it with split, but I want my class property to perform the calculation and conversion.

3
  • 3
    Create a readonly property. The get of the property will call string.Split. Commented Sep 16, 2019 at 11:18
  • @mjwills While that is correct, it's bad practice to have properties that have side effects - I'm not a fan. Commented Sep 16, 2019 at 11:20
  • @DavidG We'll have to agree to disagree. I think that generally when people talk about side effects in a property this is not necessarily the kind of scenario they are talking about. Commented Sep 16, 2019 at 11:21

1 Answer 1

0
   /* Use MyObject like below:

        var b = new MyObject();
        b.datesString = "20190819|20190830|20190915"; //set datesList to list of strings
        var ss = new List<string> { "a", "b" };
        b.datesList = ss; //set datesString to 'a|b'

    */

    public class MyObject {
        public string datesString {get;set;}
        public IEnumerable<string> datesList
        {
            get
            {
                if(string.IsNullOrEmpty(datesString))
                {
                    return null;// or new empty list
                }
                return datesString.Split('|');
            }
            set
            {
                if(value != null)
                {
                    datesString = "";
                    foreach(var s in value)
                    {
                        datesString += s + "|";
                    }
                    datesString = datesString.Substring(0, datesString.Length - 1);
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.