Home > front end >  c# Printing List Items in a class
c# Printing List Items in a class

Time:02-05

Good afternoon,

I'm trying to refresh my knowledge on c# a bit and I have a challenge to create a class called Library, with a property of List books. I have another class called Books that gets the title, author, and number of pages. The goal here is to add books from the other class to the library.

I added my books from my books class into the List from the library. I just can't seem to get it printed out in the console due to an error I will add to the bottom. I know how to print out list items normally but not for this.

Class for Book

    public class Book
{
    //This Class doesn't require fields

    //properties
    public string Title { get; set; }
    public string Author { get; set; }
    public int NumberOfPages { get; set; }

    //Constructors
    public Book() { }

    public Book(string title, string author, int numberOfPages)
    {
        Title = title;
        Author = author;
        NumberOfPages = numberOfPages;
    }

    public override string ToString()
    {
        return $"Title: {Title}\n"  
            $"Author: {Author}\n"  
            $"Number of pages: {NumberOfPages}";
    }
}

Class for Library

    public class Library
{
    //This class has no fields

    //Properties
    public List<Book> Books { get; set; }
    public string LibraryName { get; set; }
    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }

    //Constructors
    public Library() { }

    public Library(List<Book> books, string libraryName, string streetAddress, string city, string state, string zip)
    {
        Books = books;
        LibraryName = libraryName;
        StreetAddress = streetAddress;
        City = city;
        State = state;
        Zip = zip;
    }

    public override string ToString()
    {
        return $"{Books}\n\n"  
            $"Library Name: {LibraryName}\n"  
            $"Address: {StreetAddress}, {City}, {State} {Zip}";
    }
}

Method to print out class to console

    void Library()
    {
        List<Book> books = new List<Book>();
        books.Add(lordLoss);
        books.Add(harryPotter);
        Library kansasCity = new Library(books, "Kansas City Library", "3242 E 24th St", "Kansas City", "MO", "64115");
    
        Console.WriteLine(books);
}

Error Code I'm Getting

System.Collections.Generic.List`1[ClassLibrary.Book]

CodePudding user response:

You'll have to iterate over the list and print each book separately.

With for loop:

for (int i = 0; i < books.Count; i  )
{
     Book book = books[I];
     Console.WriteLine(book);
}

With foreach loop:

foreach (Book book in books)
{
     Console.WriteLine(book);
}

With List.ForEach:

books.ForEach(book => Console.WriteLine(book));

CodePudding user response:

I figured out the answer. You can just put the foreach inside of the ToString() area.

public override string ToString()
    {
        string booksInLibrary = "";

        foreach (Book b in Books)
        {
            booksInLibrary  = b   "\n";
        }

        return $"Books: \n {booksInLibrary}"  
            $"Library Name: {LibraryName}\n"  
            $"Address: {StreetAddress}, {City}, {State} {Zip}";
    }
  •  Tags:  
  • Related