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 6741349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:42:50+00:00 2026-05-26T11:42:50+00:00

I am trying to save data to ArrayList() by using following code in WinForm

  • 0

I am trying to save data to ArrayList() by using following code in WinForm application. The problem is when I do this my output in bookList.txtBookList.Text is Lab_8.Book and I can’t figure out why. Hope for some tips.

BookEntry.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Lab_8
{
    public partial class BookEntry : Form
    {
        private ArrayList books = new ArrayList();
        private BookList bookList = new BookList();

        public BookEntry()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            books.Add(new Book(txtTitle.Text, txtFirstName.Text, 
                txtLastName.Text, txtPublisher.Text, 
                float.Parse(txtPrice.Text)));
        }

        private void btnShowBooks_Click(object sender, EventArgs e)
        {
            foreach (object list in books)
            {
                bookList.txtBookList.Text += list.ToString() + "\n";
            }

            bookList.ShowDialog();
        }
    }
}

Book.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab_8
{
    class Book : Publication
    {
        private string authorFirstName, authorLastName;

        public Book()
        { }

        public Book(string title, string firstName, string lastName, string publisherName, float price)
        : base(title, publisherName, price)
        {
            this.authorFirstName = firstName;
            this.authorLastName = lastName;
        } 

        public string getAuthorName()
        {
            return authorFirstName + " " + authorLastName;
        }

        public string AuthorFirstName
        {
            get
            {
                return authorFirstName;
            }
            set
            {
                authorFirstName = value;
            }
        }
        public string AuthorLastName
        {
            get
            {
                return authorLastName;
            }
            set
            {
                authorLastName = value;
            }
        }
    }
}

Regards. HelpNeeder

  • 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-05-26T11:42:51+00:00Added an answer on May 26, 2026 at 11:42 am

    The problem here is the Book doesn’t override ToString, which means this loop:

    foreach (object list in books)
    {
      bookList.txtBookList.Text += list.ToString() + "\n";
    }
    

    Will just print out the name of the class (with the namespace), or Lab_8.Book.

    In order to correct this, you could override ToString() in Book:

    class Book : Publication
    {
       // .. Your code...
    
       public override string ToString()
       {
           return this.Title + " by " + this.AuthorFirstName + " " + this.AuthorLastName;
       }
    

    That being said, I would highly recommend converting the ArrayList to use List<Book> instead. This will allow you to refer to the books as a book directly, ie:

    foreach (Book book in books)
    {
      bookList.txtBookList.Text += book.AuthorFirstName + Environment.NewLine;
    }
    

    The only change required for this would be to change:

    private ArrayList books = new ArrayList();
    

    To:

    private List<Book> books = new List<Book>();
    

    And then also change the list as above…

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

Sidebar

Related Questions

I'm trying to create core data application. Some times when trying to save data,
I am trying to save data into cache using ASIHTTP ( http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_a_download_cache ). I
Hi, I am trying to save data using web services, jquery and json. From
I'm trying to save a data in a simple form through Hibernate using struts
I'm trying to save data with following structure: As you can see, there is
Using CakePHP 1.3 I'm trying to save some data to a MySQL database. The
In my application I save voxel-data (compressed byte array) and trying to load it
I'm trying to save data from HTML table to database using jquery to call
I am trying to save data to a database on a button push, but
I am trying to save data into the same table several times from the

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.