0

I was wondering how to pass and store values from an array to class object in C#.

here my main program code

public partial class Form1 : Form
    {
        List<Configuration> lines = new List<Configuration>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            //Read in every line in the file
            using (StreamReader reader = new StreamReader("file.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string textfile = line;
                    string[] array = new string[] { "\\n" };
                    string[] parts = textfile.Split(array, StringSplitOptions.RemoveEmptyEntries);

                    ///////////////////////////////////////////////////
                    for (int i = 0; i < parts.Length; i++)
                    {
                       lines.Add(new Configuration(parts[i],0));

                    }
                    //lines.Add(new Configuration(line));

                    //listBox1.Items.Add(line);
                }

            }
            listBox1.DataSource = lines;
            listBox1.DisplayMember = "CompanyName";
        }
    }

my class object code

class Configuration
    {
        string _CompanyName;
        int _Employees;

        public Configuration(string companyname, int number_of_Employees)
        {
            _CompanyName = companyname;
            _Employees = number_of_Employees;
        }

        //program properties and validation
        public string CompanyName
        {
            set
            {
                _CompanyName = value;
            }
            get
            {
                return _CompanyName;
            }
        }// End of levelname validation

        //program properties and validation
        public int EmployeesNumber
        {
            set
            {
                _Employees = value;
            }
            get
            {
                return _Employees;
            }
        }// End of levelname validation
    }

At the moment the program reads a text file which has a list of companies and number of employees per company. structure like this

Microsoft\n92200
Google\n33077
Apple\n60400
IBM\n426751
Facebook\n3000

when the program runs it splits the company names and number of employees apart into an array. that part is working fine, it just store everything into the String companyName. I an error when ever I try to modify it to store the value in different fields in the class object.

2
  • 4
    " I an error" - and that error is? Commented Apr 21, 2012 at 7:04
  • Side note: \n is very unusual separator, why not to use something more traditional like "," or simply space? Unless you really want to conuse readers of your code and configuration by using what normally represents "new line" character as separtor of columns in single row... Commented Apr 21, 2012 at 7:18

2 Answers 2

4

When you split your line by "\\n" with the lines you have in your example, you will get a single array with two items. The first item is what was to the left of the \n and the second is the one to the right.

In your code, you're looping through those two items and creating new Configuration objects passing each in as the first parameter of the constructor.

The constructor takes in two parameters, the name and the number. On each of the lines, the first item of the split is the name and the second item is the number (in a string representation). Rather than passing both parts individually as the first parameter to the constructor, both need to be used when you create your objects. Though you have the number as a string so you will need to convert it to an integer first to be able to use it in your constructor...

Once you get that part sorted, I believe that should fix the problems that you are having (whatever that is).

Sign up to request clarification or add additional context in comments.

Comments

2
var configs = 
    from line
    in reader.ReadToEnd().Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
    let pair = line.Split(new[] { "\\n"}, StringSplitOptions.RemoveEmptyEntries)
    select new Configuration(companyname: pair.First(), number_of_Employees: int.Parse(pair.Last()));

lines = configs.ToList();

2 Comments

what if I wanna pass more than two value to the class object, what do I do cause, currently it gets the first and last elements.
Then rename "pair" to "parts" and use indexing instead of First() and Last() in such case. "pair" is an array anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.