Reputation: 133
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.
Upvotes: 0
Views: 1587
Reputation: 134811
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).
Upvotes: 4
Reputation: 157
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();
Upvotes: 2