none
Subclasses constructors RRS feed

  • Question

  • Can somebody help me fix my program? I built constructors for subclasses but the program still asks for them.

    using System; using System.Collections.Generic; namespace Library { public class Book { public string Title; public string Author; public string Language; public Book(string indataTitle, string indataAuthor, string indataLanguage) { Title = indataTitle; Author = indataAuthor; Language = indataLanguage; } public override string ToString() { var i = 0; return String.Format("\n\t" + i + " " + "\"{0}\" av {1}, language: {2} ({3}).", Title, Author, Language); } } public class Novel : Book { public string Category; public Novel(string indataTitle, string indataAuthor, string indataLanguage) : base(indataTitle, indataAuthor, indataLanguage) { Category = "Novel"; } } public class Literature : Book { public string Category; public Literature(string indataTitle, string indataAuthor, string indataLanguage) : base(indataTitle, indataAuthor, indataLanguage) { Category = "Literature"; } } public class ShortStory : Book { public string Category; public ShortStory(string indataTitle, string indataAuthor, string indataLanguage) : base(indataTitle, indataAuthor, indataLanguage) { Category = "Short story"; } } class Program { static void Main(string[] args) { List<Book> library = new List<Book>(); string bookTitle = ""; string bookWriter = ""; string bookLanguage = ""; bool play = true; while (play) { Console.WriteLine("\n\tWelcome to the library!"); Console.WriteLine("\t[1]Register a new book"); Console.WriteLine("\t[2]Show all the books"); Console.WriteLine("\t[3]End"); Console.Write("\n\t"); int choice; Int32.TryParse(Console.ReadLine(), out choice); switch (choice) { case 1: Console.Write("\n\tWrite the title: "); bookTitle = Console.ReadLine(); Console.Write("\n\tWrite the author: "); bookWriter = Console.ReadLine(); Console.Write("\n\tWrite the language: "); bookLanguage = Console.ReadLine(); Console.Write("\n\tIs the book a [1]Novel, [2]Literature or a [3]Short story? "); int bookCategory; Int32.TryParse(Console.ReadLine(), out bookCategory); Console.WriteLine("\n\tSaved!"); Book myNewBok = new Book(bookTitle, bookWriter, bookLanguage); /*nyBok.Titel = bokTitel; nyBok.Skribent = bokFörfattare; nyBok.Språk = bokSpråk;*/ Novel myNovel = new Novel(); Literature myLiterature = new Literature(); ShortStory myShortStory = new ShortStory(); if (bookCategory == 1) library.Add(myNovel); if (bookCategory == 2) library.Add(myLiterature); if (bookCategory == 3) library.Add(myShortStory); else Console.WriteLine("Please choose numbers 1-3"); break; case 2: int max = library.Count; if (max > 0) for (int i = 0; i < library.Count; i++) { Console.WriteLine(library[i].ToString()); } else Console.WriteLine("\n\tThe library is empty."); Console.ReadLine(); break; case 3: Console.WriteLine("Thank you for using the library!"); Console.ReadLine(); play = false; break; default: Console.WriteLine("Please choose only numbers 1-3"); Console.ReadLine(); break; } } } } }


    Wednesday, January 15, 2020 12:15 PM

Answers

  • Hi MorenainHanoi,

    Thank you for posting here.

    If you provide a constructor for your class, C# will not create a parameterless constructor.

    Use a constructor with parameters:

                            Novel myNovel = new Novel(bookTitle, bookWriter, bookLanguage);
                            Literature myLiterature = new Literature(bookTitle, bookWriter, bookLanguage);
                            ShortStory myShortStory = new ShortStory(bookTitle, bookWriter, bookLanguage);

    Besides, you can only use 'Book' class to do it.

        public class Book
        {
            public string Title;
            public string Author;
            public string Language;
            public string Category;
    
            public Book(string indataTitle, string indataAuthor, string indataLanguage)
            {
                Title = indataTitle;
                Author = indataAuthor;
                Language = indataLanguage;
            }
            public override string ToString()
            {
                return String.Format("\"{0}\" av {1}, language: {2} {3}.",
                    Title, Author, Language, Category);
            }
        }
                        case 1:
    
                            Console.Write("\n\tWrite the title:  ");
                            bookTitle = Console.ReadLine();
                            Console.Write("\n\tWrite the author: ");
                            bookWriter = Console.ReadLine();
                            Console.Write("\n\tWrite the language: ");
                            bookLanguage = Console.ReadLine();
                            Console.Write("\n\tIs the book a [1]Novel, [2]Literature or a [3]Short story? ");
                            int bookCategory;
                            Int32.TryParse(Console.ReadLine(), out bookCategory);
                            Console.WriteLine("\n\tSaved!");
    
                            Book myNewBok = new Book(bookTitle, bookWriter, bookLanguage);
                            if (bookCategory == 1)
                                myNewBok.Category = "Novel";
                            if (bookCategory == 2)
                                myNewBok.Category = "Literature";
                            if (bookCategory == 3)
                                myNewBok.Category = "Short story";
                            
                            else
                                Console.WriteLine("Please choose numbers 1-3");
                            library.Add(myNewBok);
                            break;
                        case 2:
                            int max = library.Count;
    
                            if (max > 0)
    
                                for (int i = 0; i < library.Count; i++)
                                {
                                    Console.WriteLine("\n\t" + i + " " + library[i].ToString());
                                }
    
                            else
                                Console.WriteLine("\n\tThe library is empty.");
                            Console.ReadLine();
                            break;

    Hope it can help you.

    Best Regards,

    Xingyu Zhao

      

     


    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 16, 2020 7:57 AM
    Thursday, January 16, 2020 2:40 AM
    Moderator