none
How to get a List from a public void method RRS feed

  • Question

  • I have saved a book using a public void method:

    myLibrarian.SaveBook(newBook);

    So my newBook is saved, and i want to show the list of saved books that i made.  I also have a public void method for it:

    myLibrarian.GetBook(newBook);

    My question is how do i make an instance where it will show me the complete list of books that i saved?


     public class Librarian
        {
            private List<Book> bookList = new List<Book>();
            public void SaveBook(Book book)
            {
                bookList.Add(book);
            }
          
            public void GetBook(Bok allBook)
            {
                foreach (Book book in bokList)
                {
                    Console.WriteLine(book.ToString());
                }
            }
    }

    Thursday, January 30, 2020 2:05 PM

Answers

  • Thanks Karen, will look into this also :)
    So what was the solution you deemed as working for you? if one or more replies to your question solved this you should mark those replies as answers and if one or more assisted click the point button.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 3:41 PM
    Thursday, January 30, 2020 3:35 PM
    Moderator
  • Perhaps

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var librarian = new Librarian();
    
                librarian.AddRange(new List<Book>() { new Book("Book1"), new Book("Book2") });
    
                var currentBooks = librarian.ViewBooks();
                Console.WriteLine(currentBooks);
            }
        }
    
        class Book
        {
            string name;
            public Book(string bookName)
            {
                name = bookName;
            }
            public string Name()
            {
                return name;
            }
    
            public override string ToString()
            {
                return Name();
            }
        }
    
        class Librarian
        {
            private List<Book> bookList;
    
            public Librarian()
            {
                bookList = new List<Book>();
            }
            public List<Book> BookList => bookList;
    
            public void Add(Book book)
            {
                bookList.Add(book);
            }
    
            public void AddRange(List<Book> list)
            {
                bookList.AddRange(list.ToArray());
            }
    
            public string ViewBooks()
            {
                var sb = new StringBuilder();
                foreach (var book in bookList)
                {
                    sb.AppendLine(book.ToString());
                }
    
                return sb.ToString();
            }
    
    
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 3:38 PM
    Thursday, January 30, 2020 2:22 PM
    Moderator
  • Thanks Karen, will look into this also :)
    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 3:22 PM
    Thursday, January 30, 2020 3:22 PM

All replies

  • Without exposing the private list, you could do method that returns enumerable of books over the private list.

    public class Librarian
    {
           	private List<Book> bookList = new List<Book>();
            public void SaveBook(Book book)
            {
                bookList.Add(book);
            }
    
    	public IEnumerable<Book> GetBooks()
            {
    	    foreach (Book book in bookList)
    		yield return book;
            }
          
            public void GetBook(Bok allBook)
            {
                foreach (Book book in bokList)
                {
                    Console.WriteLine(book.ToString());
                }
            }
    }
    
    // Usage:
    foreach (Book book in myLibrarian.GetBooks())
    {
        Console.WriteLine(book.ToString());
    }

    Thursday, January 30, 2020 2:10 PM
  • Perhaps

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var librarian = new Librarian();
    
                librarian.AddRange(new List<Book>() { new Book("Book1"), new Book("Book2") });
    
                var currentBooks = librarian.ViewBooks();
                Console.WriteLine(currentBooks);
            }
        }
    
        class Book
        {
            string name;
            public Book(string bookName)
            {
                name = bookName;
            }
            public string Name()
            {
                return name;
            }
    
            public override string ToString()
            {
                return Name();
            }
        }
    
        class Librarian
        {
            private List<Book> bookList;
    
            public Librarian()
            {
                bookList = new List<Book>();
            }
            public List<Book> BookList => bookList;
    
            public void Add(Book book)
            {
                bookList.Add(book);
            }
    
            public void AddRange(List<Book> list)
            {
                bookList.AddRange(list.ToArray());
            }
    
            public string ViewBooks()
            {
                var sb = new StringBuilder();
                foreach (var book in bookList)
                {
                    sb.AppendLine(book.ToString());
                }
    
                return sb.ToString();
            }
    
    
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 3:38 PM
    Thursday, January 30, 2020 2:22 PM
    Moderator
  • Okay will try it thanks :)
    Thursday, January 30, 2020 2:55 PM
  • Thank you it works.  Moreover i am making  if-clauses that will return that

    if (bookList.Count >= 1)// Here bookList.Count isnt working so what should I use?

    foreach (Book book in myLibrarian.GetBooks())
    {
        Console.WriteLine(book.ToString());
    }

    else

    Console.WriteLine("Library is empty.")

    Thursday, January 30, 2020 3:19 PM
  • Thanks Karen, will look into this also :)
    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 3:22 PM
    Thursday, January 30, 2020 3:22 PM
  • Thanks Karen, will look into this also :)
    So what was the solution you deemed as working for you? if one or more replies to your question solved this you should mark those replies as answers and if one or more assisted click the point button.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 3:41 PM
    Thursday, January 30, 2020 3:35 PM
    Moderator
  • Some of your suggestions are definitely useful, as there can be many ways to solve problems.  Thanks :)
    Thursday, January 30, 2020 3:41 PM