Populating a Classes Variables using values from a split string

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

So I have this code:

public class myClass
{

    public string A { get; set; }
    public string B{ get; set; }
    //and so on
}

I want to add a constructor that takes a string input and have that constructor split the string on ‘,’then use the split values to populate the class variables

What I Have Tried

I have tried to make a string with my variables values like so:

public void myVoid()
    {


        string s = "A,B,..etc";
        string[] values = s.Split(',');

        foreach (string item in values)
        {
            Console.WriteLine(item);
            Console.ReadKey();
        }
    }

This returns my values one by one on each key press but this is just me getting my head around it.

So to clarify: I want to add a constructor that takes a string input and have that constructor split the string on ‘,’then use the split values to populate the class variables

solved 0
4 Answer 4 views 0

Answers ( 4 )

    0
    December 3, 2016 at 8:43 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    Is this what you want?

    public class myClass
    {
        public myClass(string input)
        {
             var ar = input.Split(',');
             if (ar.Length >= 2)
             {
                 A = ar[0];
                 B = ar[1];
             }
        }
    
        public string A { get; set; }
        public string B{ get; set; }
        //and so on
    }
    

    Use:

    var myclass = new myClass("some,thing");
    
    Best answer
    0
    December 3, 2016 at 8:43 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    A simple approach would be:

        public class myClass
        {
           public string A { get; set; }
           public string B{ get; set; }
           public myClass()
           {
             //default constructor
           }
           public myClass(string S)
           {
             string[] values = S.Split(',');
             if (values.Length>=2)
             {
                this.A=values[0];
                this.B=values[1];
             }
           }
         }
    

    However your goal is a bit fuzzy. E.g which value of splited string would appended on A and which in B?

    0
    December 3, 2016 at 8:43 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    Do you want this –

    public class myClass
    {
        private string _a, _b;
    
        public myClass(string args)
        {
            _a = args.Split(',')[0];
            _b = args.Split(',')[1];
        }
    
        public string A { get { return _a; } set { _a = value; } }
        public string B { get { return _b; } set { _b = value; } }
        //and so on
    }
    

    enter image description here

    0
    December 3, 2016 at 8:43 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel
     public string A { get; set; }
        public string B { get; set; }
    
        public Sample(string strSplit) {
            var values = strSplit.Split(',');
            A = values[0];
            B = values[1];
        }
    Sample dd = new Sample("James,Jonathan");
    

    better use reflection

Leave an answer

Browse

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>