【发布时间】:2011-12-20 21:51:51
【问题描述】:
我在从 ArrayList 检索数据并将它们显示到文本框中时遇到问题。我收到一个错误:Unable to cast object of type 'Lab_9.Book' to type 'Lab_9.Magazine'。我尝试使用 2 个 foreach 循环,但这似乎是不可能的。我怎样才能避免这个问题?
问题出现在这里:
// Displaying all Book objects from pubs ArrayList.
foreach (Book list in pubs)
{
bookNumber++; // Count books from the begining.
// Displaying and formating the output
// in txtBookList textbox.
bookList.txtBookList.Text +=
"=== Book " + bookNumber + " ===" + Environment.NewLine +
list.Title + Environment.NewLine +
list.getAuthorName() + Environment.NewLine +
list.PublisherName + Environment.NewLine +
"$" + list.Price + Environment.NewLine
+ Environment.NewLine;
}
问候。
【问题讨论】:
标签: c# object foreach arraylist