0

I have the following code:

public partial class Form1 : Form
{
    const int MaxStudents = 4;

    public Form1()
    {
        InitializeComponent();
    }

   private void Form1_Load(object sender, EventArgs e)
   {
     Student[] studentList;
     studentList = new Student[4];

     studentList[0] = new Student(51584, 17);
     studentList[1] = new Student(51585, 19);
     studentList[2] = new Student(51586, 15);
     studentList[3] = new Student(51587, 20);

        for (int i = 0; i < MaxStudents; i++)
        {
            lstStudents.Items.Add(studentList.ToString()[i]);
        }
    }

EDIT: In the Student class I have:

public Student(int id, int age) {
this.id = id;
this.age = age;
}

public override string ToString()
{
    return string.Format("ID: {0} - Age: {1}", this.id, this.age);
} 

Then in the form load I have:

private void Form1_Load(object sender, EventArgs e)
{
Student[] studentList;
studentList = new Student[4];

studentList[0] = new Student(51584, 17);
studentList[1] = new Student(51585, 19);
studentList[2] = new Student(51586, 15);
studentList[3] = new Student(51587, 20);

lstStudents.Items.AddRange(studentList);
}

I was wondering how I would output the parameters of each object in the array to the listbox. How would I make it so that each object is displayed in the listbox like so:

ID: 51584 - Age: 17

I'm not too sure how to basically convert the parameters into plain text to be listed in the listbox whilst adding additional text before the parameters (Like I did with 'id:', the hyphen and 'Age:')

Sorry for the long winded question but thought I'd explain as best as I can.

4
  • @Jonesy Pasted the wrong code in. Basically I want to be able to take integers from the parentheses and be able to output them as items in the listbox. I assume I'd have to convert them to a string or something along those lines but I want to be able to add text before or after the text i.e. so I can output ID: 51584 rather than just the number itself. Commented Oct 30, 2013 at 18:31
  • Check my answer, which will solve all your issues Commented Oct 30, 2013 at 19:23
  • @T.S. I think overriding ToString is the right way to go about it but it's getting it to work as intended that seems to be the issue. Commented Oct 30, 2013 at 19:26
  • Overriding string is NOT a way for this. Overriding string is done for more complex issues, outside of scope of this. My code is 4 lines that solve your entire problem, appropriately. Commented Oct 30, 2013 at 19:31

3 Answers 3

1

If you want Student to be shown that way anytime you want a string value, the easiest way is to override ToString:

public override string ToString()
{
    return string.Format("Name: {0} - Age: {1}", this.Name, this.Age);
} 

Then you can just do

lstStudents.Items.Add(studentList[i]);

The benefit of adding an object instead of a string is that the SelectedItem property of the listbox will be the object instead of just the string representation.

Or you can format the string that's sent to the listbox:

lstStudents.Items.Add(string.Format("Name: {0} - Age: {1}", studentList[i].Name, studentList[i].Age));
Sign up to request clarification or add additional context in comments.

5 Comments

Additionally, the OP has to use studentList[i].ToString() in his code instead of "studentList.ToString()[i]".
@D Stanley so would this apply to integers as well? Like in my updated post could I do `return string.Format("ID: {0} - Age: {1}", this.ID, this.Age); to output the integers? I assume you can?
@user2929108 Yes string.Format will accept integers as well. It can accept any object in fact.
Yes, you can use integers as well.
@DStanley I've put the override into Student.cs and the listbox code itself is in Form1.cs in the FormLoad event but it's not outputting anything into the listbox. Do I have the code in the wrong place?
0

Have a close look at this line:

lstStudents.Items.Add(studentList.ToString()[i]);

You write studentList.ToString()[i]. This means you call ToString on studentList instead of the students. It should be lstStudents.Items.Add(studentList[i].ToString()) (with the indexer [i] placed behind studentList).

Assuming that you have overwritten ToString in the Student class you can add all students in a single call:

private void Form1_Load(object sender, EventArgs e)
{
    Student[] studentList;
    studentList = new Student[4];

    studentList[0] = new Student(51584, 17);
    studentList[1] = new Student(51585, 19);
    studentList[2] = new Student(51586, 15);
    studentList[3] = new Student(51587, 20);

    lstStudents.Items.AddRange(studentList);
}

4 Comments

See my edit. I've overridden ToString in the Student class but it's not outputting anything to the list box
Your updated code should work. Are you sure that Form1_Load is called?
It must handle the Load event. See the events in the properties window of Form1.
D'oh! I miss the most obvious things sometimes! Thank you very much :)
0
// Class student - create property
public string Display
{
   get { return string.Format("ID: {0} - Age: {1}", this.ID, this.Age); }
} 

 // In the form class - set listbox
 lstStudents.Datasource = StudentArray;
 lstStudents.DisplayMember = "Display";
 lstStudents.ValueMember = "ID";

 // The benefit of this is that later you can do this
 Student s = (Student)lstStudents.SelectedItem
 // Now you have access to full student info

Comments

Your Answer

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