I am having a problem with displaying a content from a ArrayList() stored as such:
private void btnEnter_Click(object sender, EventArgs e)
{
public ArrayList books = new ArrayList();
books = new ArrayList();
// first I am just using lebels for easier input.
books.Add(new Book(label1.Text, label2.Text, label3.Text, label4.Text, float.Parse(label5.Text)));
}
As we see I am using a constructor to store data with 5 elements. The problem is that I am not sure how to display the data using foreach loop:
public ArrayList books;
public BookList()
{
InitializeComponent();
foreach (string data in books)
txtBookList.Text = data.ToString();
}
I am trying to display the content in multiline textbox and I am not sure what I’m exacly doing wrong. Any tips?
Here is the code for Book() constructor:
public Book(string title, string firstName, string lastName, string publisherName, float price)
: base(title, publisherName, price)
{
this.authorFirstName = firstName;
this.authorLastName = lastName;
}
— EDIT —
One person pointed out that I do want to display the ArrayList as a objects. How would I do it?
Regards.
HelpNeeder
You have a couple of problems here – the main one being that your ArrayList books isn’t a collection of strings, it’s a collection of book objects. Therefore your foreach loop needs to iterate through book objects instead of strings:
The second problem is that you are trying to assign a string value to the txtBookList variable, which I’m assuming is a textbox. Try using the Text property instead and use the += operator to append new strings to the value, like above.