none
How to input strings into a private List<> RRS feed

  • Question

  •  

    Can somebody help me how to input strings in a private List<Book> in the main method??

    Thank you so much in advance.

    -Morena

    public class Librarian { private List<Book> bookList; public void RegisterBook(List<Book> myRegisteredBook) { bookList = myRegisteredBook; } public List<Book> RegisteredBooks() { return bookList; } } class Program { static void Main(string[] args) { Bok myBook = new Book(); string bookTitle; string bookAuthor; int PublishingYear; Librarian newBook = new Libararian(); newBook.RegisterBook();//I need help here!!! List<Book> newBooks = newBook.RegisteredBooks();


    Friday, January 24, 2020 7:21 PM

Answers

  • The thing is:

    So a Librarian owns a list of books.

    The list never get created, so first, change 

    private List<Book> bookList;

    to

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

    Now, Register should look something like:

            internal void RegisterBook(Book book)
            {
                bookList.Add(book);
            }
    This is NOT tested, but should get you fairly close.


    I'd rather live with false hope than with false despair.

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Saturday, January 25, 2020 12:30 AM
  • This is one step closer

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        public class Librarian
        {
            private List<Book> bookList;
    
            public void RegisterBook(List<Book> myRegisteredBook)
            {
                bookList = myRegisteredBook;
            }
            public List<Book> RegisteredBooks()
            {
                return bookList;
            }
    
            internal void RegisterBook()
            {
    
            }
        }
    
        public class Book
        {
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Book myBook = new Book();
                string bookTitle;
                string bookAuthor;
                int PublishingYear;
    
                Librarian newBook = new Librarian();
                newBook.RegisterBook();//I need help here!!!
                List<Book> newBooks = newBook.RegisteredBooks();
            }
        }
    }

    Then another step

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        public class Librarian
        {
            private List<Book> bookList;
    
            public void RegisterBook(List<Book> myRegisteredBook)
            {
                bookList = myRegisteredBook;
            }
            public List<Book> RegisteredBooks()
            {
                return bookList;
            }
    
            internal void RegisterBook(Book book)
            {
                // do register process 
            }
        }
    
        public class Book
        {
            public string Title { get; set; }
            public string Author { get; set; }
            public int PublishedYear { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Book myBook = new Book() {Title = "", Author = "", PublishedYear = 2020};
        
                Librarian newBook = new Librarian();
                newBook.RegisterBook(myBook);//I need help here!!!
                List<Book> newBooks = newBook.RegisteredBooks();
            }
        }
    }
    


    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


    Friday, January 24, 2020 7:30 PM
    Moderator
  • I do not understand what input strings in a private List<Book> means.

    This is obviously a student exercise and therefore it would help you most to help you understand instead of giving you complete code without any assistance understanding it.

    <book>Your Main method seems incomplete. Perhaps your code is only missing a couple of closing braces but you should learn to include things like that so that we know it is complete.</book>

    <book>We do not know what your program is supposed to do so we cannot give you the best answer.</book>

    In your Librarian class the name of the RegisterBook method is singular but the method will add as many books to the list as is in the myRegisteredBook parameter; the name myRegisteredBook is singular but it is a list therefore it can have many books in it.

    You do not show us the definition of Book but I assume the members are bookTitle, bookAuthor, and PublishingYear. So you need to put those values into those members of the myBook instance of Book. Next you need to fix RegisterBook so that it takes just one book, not a list. Then when you call that method you just pass your myBook instance of Book.

    What part(s) of that do you need help with?



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Saturday, January 25, 2020 3:37 AM
  • Hi Sam,

    You seem to get my problem.  The private List Book was given in the exercise so the Register a Book method was also given like this below.  However I had difficulty how to put in the value of bookTitle, bookAuthor, and PublishingYear in the instance of Book.  Can you help me how to do it?  I would really appreciate it. 

    public void RegisterBook(List<Book> myRegisteredBook) { bookList = myRegisteredBook; }

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Saturday, January 25, 2020 11:30 AM
  • Both Karen and Bryan have provided pieces. As Bryan says, you need to create bookList. Bryan also shows how to pass a single book and add it to the list. I was going to attempt to help you understand how to do that so you understand better and that is more work.

    Another problem that is fundamental to your question is how to initialize a Book instance with data. There are at least three ways to do that. Karen shows:

    Book myBook = new Book() {Title = "", Author = "", PublishedYear = 2020};

    That is a possibility but I think bookTitle, bookAuthor and PublishingYear must be public for that to work and I think it is better to learn more common syntax first.

    Unfortunately Microsoft wants to limit us to the way they think is best. They think we must use a constructor to initialize class instances. It is a good solution but they just do not show examples of other possibilities in their tutorials. See Classes - C# Programming Guide | Microsoft Docs. Look at the example; it shows a constructor that has name as its argument and constructs the Person instance using that argument. You can create a constructor for Book that has bookTitle, bookAuthor and PublishingYear as arguments. I think that is the only solution of you want bookTitle, bookAuthor and PublishingYear to be private. Introduction to C# classes - The complete C# tutorial is another example of that and see Classes and objects - Introduction to C# tutorial | Microsoft Docs.

    Another way to initialize class instances is as in C# - Classes - Tutorialspoint but that also requires bookTitle, bookAuthor and PublishingYear to be public.

    I have created a working version of your code. Try to apply the preceding modifications and if it does not work then post what you have and I can compare what I have to what you have.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by MorenainHanoi Wednesday, January 29, 2020 7:18 AM
    Saturday, January 25, 2020 9:03 PM
  • Hi MorenainHanoi,

    I noticed that the method "RegisterBook" has a "List<Book>" type parameter. But you did not provide corresponding parameter when calling this method. Maybe you can modify the code like this.

        newBook.RegisterBook(new List<Book> { new Book("Book1"), new Book("Book2") });

    Another way is that you can define a property to access the field "bookList".

        class Book
        {
            string name;
            public Book(string n)
            {
                name = n;
            }
            public string GetName()
            {
                return name;
            }
        }
    
        class Librarian
        {
            private List<Book> bookList;
            public List<Book> BookList
            {
                get { return bookList; }
                set { bookList = value; }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Librarian newBook = new Librarian();
                newBook.BookList = new List<Book> { new Book("Book1"), new Book("Book2") };
                foreach (Book book in newBook.BookList)
                {
                    Console.WriteLine(book.GetName());
                }
                Console.ReadKey();
            }
        }

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Monday, January 27, 2020 5:42 AM

All replies

  • This is one step closer

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        public class Librarian
        {
            private List<Book> bookList;
    
            public void RegisterBook(List<Book> myRegisteredBook)
            {
                bookList = myRegisteredBook;
            }
            public List<Book> RegisteredBooks()
            {
                return bookList;
            }
    
            internal void RegisterBook()
            {
    
            }
        }
    
        public class Book
        {
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Book myBook = new Book();
                string bookTitle;
                string bookAuthor;
                int PublishingYear;
    
                Librarian newBook = new Librarian();
                newBook.RegisterBook();//I need help here!!!
                List<Book> newBooks = newBook.RegisteredBooks();
            }
        }
    }

    Then another step

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        public class Librarian
        {
            private List<Book> bookList;
    
            public void RegisterBook(List<Book> myRegisteredBook)
            {
                bookList = myRegisteredBook;
            }
            public List<Book> RegisteredBooks()
            {
                return bookList;
            }
    
            internal void RegisterBook(Book book)
            {
                // do register process 
            }
        }
    
        public class Book
        {
            public string Title { get; set; }
            public string Author { get; set; }
            public int PublishedYear { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Book myBook = new Book() {Title = "", Author = "", PublishedYear = 2020};
        
                Librarian newBook = new Librarian();
                newBook.RegisterBook(myBook);//I need help here!!!
                List<Book> newBooks = newBook.RegisteredBooks();
            }
        }
    }
    


    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


    Friday, January 24, 2020 7:30 PM
    Moderator
  • The thing is:

    So a Librarian owns a list of books.

    The list never get created, so first, change 

    private List<Book> bookList;

    to

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

    Now, Register should look something like:

            internal void RegisterBook(Book book)
            {
                bookList.Add(book);
            }
    This is NOT tested, but should get you fairly close.


    I'd rather live with false hope than with false despair.

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Saturday, January 25, 2020 12:30 AM
  • I do not understand what input strings in a private List<Book> means.

    This is obviously a student exercise and therefore it would help you most to help you understand instead of giving you complete code without any assistance understanding it.

    <book>Your Main method seems incomplete. Perhaps your code is only missing a couple of closing braces but you should learn to include things like that so that we know it is complete.</book>

    <book>We do not know what your program is supposed to do so we cannot give you the best answer.</book>

    In your Librarian class the name of the RegisterBook method is singular but the method will add as many books to the list as is in the myRegisteredBook parameter; the name myRegisteredBook is singular but it is a list therefore it can have many books in it.

    You do not show us the definition of Book but I assume the members are bookTitle, bookAuthor, and PublishingYear. So you need to put those values into those members of the myBook instance of Book. Next you need to fix RegisterBook so that it takes just one book, not a list. Then when you call that method you just pass your myBook instance of Book.

    What part(s) of that do you need help with?



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Saturday, January 25, 2020 3:37 AM
  • Hi Sam,

    You seem to get my problem.  The private List Book was given in the exercise so the Register a Book method was also given like this below.  However I had difficulty how to put in the value of bookTitle, bookAuthor, and PublishingYear in the instance of Book.  Can you help me how to do it?  I would really appreciate it. 

    public void RegisterBook(List<Book> myRegisteredBook) { bookList = myRegisteredBook; }

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Saturday, January 25, 2020 11:30 AM
  • Both Karen and Bryan have provided pieces. As Bryan says, you need to create bookList. Bryan also shows how to pass a single book and add it to the list. I was going to attempt to help you understand how to do that so you understand better and that is more work.

    Another problem that is fundamental to your question is how to initialize a Book instance with data. There are at least three ways to do that. Karen shows:

    Book myBook = new Book() {Title = "", Author = "", PublishedYear = 2020};

    That is a possibility but I think bookTitle, bookAuthor and PublishingYear must be public for that to work and I think it is better to learn more common syntax first.

    Unfortunately Microsoft wants to limit us to the way they think is best. They think we must use a constructor to initialize class instances. It is a good solution but they just do not show examples of other possibilities in their tutorials. See Classes - C# Programming Guide | Microsoft Docs. Look at the example; it shows a constructor that has name as its argument and constructs the Person instance using that argument. You can create a constructor for Book that has bookTitle, bookAuthor and PublishingYear as arguments. I think that is the only solution of you want bookTitle, bookAuthor and PublishingYear to be private. Introduction to C# classes - The complete C# tutorial is another example of that and see Classes and objects - Introduction to C# tutorial | Microsoft Docs.

    Another way to initialize class instances is as in C# - Classes - Tutorialspoint but that also requires bookTitle, bookAuthor and PublishingYear to be public.

    I have created a working version of your code. Try to apply the preceding modifications and if it does not work then post what you have and I can compare what I have to what you have.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by MorenainHanoi Wednesday, January 29, 2020 7:18 AM
    Saturday, January 25, 2020 9:03 PM
  • Hi MorenainHanoi,

    I noticed that the method "RegisterBook" has a "List<Book>" type parameter. But you did not provide corresponding parameter when calling this method. Maybe you can modify the code like this.

        newBook.RegisterBook(new List<Book> { new Book("Book1"), new Book("Book2") });

    Another way is that you can define a property to access the field "bookList".

        class Book
        {
            string name;
            public Book(string n)
            {
                name = n;
            }
            public string GetName()
            {
                return name;
            }
        }
    
        class Librarian
        {
            private List<Book> bookList;
            public List<Book> BookList
            {
                get { return bookList; }
                set { bookList = value; }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Librarian newBook = new Librarian();
                newBook.BookList = new List<Book> { new Book("Book1"), new Book("Book2") };
                foreach (Book book in newBook.BookList)
                {
                    Console.WriteLine(book.GetName());
                }
                Console.ReadKey();
            }
        }

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MorenainHanoi Thursday, January 30, 2020 8:16 AM
    Monday, January 27, 2020 5:42 AM