Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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

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

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

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:38:59+00:00 2026-06-02T07:38:59+00:00

I was wondering how to pass and store values from an array to class

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:39:00+00:00Added an answer on June 2, 2026 at 7:39 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i was wondering how to pass an object to a partial, and then again
So, I'm wondering how to pass parameters to a perl script from a .bat
I'm wondering if someone could look over my code. I'm trying to pass a
I'm wondering if there's a way for Binding object to store reference to an
I'm creating a data store that will load the data from server. I'm wondering
I was wondering if its possible to pass data stored in a javascript array
I'm wondering if it's possibile to pass (any way is ok, also hacks) a
I am wondering if there is a way to pass a variable to a
I'm wondering the opinion of what is the best way to pass a lot
I'm wondering if there is a cleaner way to pass multiple variables in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.