Object.Property错误“非静态属性需要的对象引用。”对于Book.Title.get

时间:2018-02-01 03:37:22

标签: c#

我已经搜索过,可能还不够长,但仍然搜索,并且找不到像我这样的object.property错误。

  

“非静态字段,方法或属性需要对象引用”

我无法找到修复程序。差不多3个小时。 我使用线性搜索并搜索书名和搜索输入之间的匹配。假设Book.Title会起作用,它会抛出错误。

public class Library
    {

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

        public Library ()
        {

        }

        public void AddBook(string title, string author, int id)
        {
            Book b = new Book (title, author, id);
        }

        public int Findbook(string Title)
        {
            string search = Title;
            for (int i = 0; i < books.Count; i++)  
            {             
                //Error occurs here
                if (search == Book.Title) 
                {
                    return i;
                }

                return -1;
            }
        }

        public string DisplayBook()
        {
            return "";
        }

        public int booksCount()
        {
            int nextId = books.Count + 2;
            return nextId;
        }

        public void removeid(int x)
        {
            int removeid = x + 1;
            books.RemoveAt (removeid);
        }

        public override string ToString()
        {
            string t = "";
            foreach (Book b in books) 
            {
                t += (b + "\n");
            }

            return t;
        }
    }
} 

书类

{
       public class Book
    {
        private string author;
        private string title;
        private int id;

        public string Author
        {
            get {
                return this.author;
            }
            set {
                author = value;
            }
        }

        public string Title {
            get {
                return this.title;
            }
            set {
                title = value;
            }
        }

        public int Id {
            get {
                return this.id;
            }
            set {
                id = value;
            }
        }



        public Book (string title, string author, int id)
        {
            this.author = author;
            this.title = title;
            this.id = id;
        }

        public override string ToString ()
        {
            return title + "\t" + author + "\t" + id;
        }
    }
}

主类

    class MainClass
    {

        public static void Main (string[] args)
        {

            Library lib = new Library ();

            lib.AddBook ("C# programming", "Gesick", 4);

            lib.AddBook ("java programming", "Roth", 2);

            lib.AddBook ("C++ programming", "Franklin", 1);

            lib.AddBook ("unity programming", "Preston", 3);

            lib.AddBook ("graphics & multimedia", "Chastine", 5);

            printMenu ();

            string p = Console.ReadLine ();

            p = p.ToUpper ();

            char pick = p [0];



            while (pick != 'Q') {

                switch (pick) {

                case 'A':

                    Console.WriteLine ("What is title of the book?");
                    string title = Console.ReadLine ();
                    Console.WriteLine ("Who is the author?");
                    string author = Console.ReadLine ();
                    int id = lib.booksCount ();
                    lib.AddBook (title, author, id);
                    //insert code here for adding a book

                    break;

                case 'S':
                    Console.WriteLine ("Please Enter a book by Title");
                    string search = Console.ReadLine ();
                    int searched = lib.Findbook(search);
                    if (searched > -1)
                    {
                        Console.WriteLine ("Book Found!");

                    }
                    if (searched == -1)
                    {
                        Console.WriteLine ("Sorry that book is not in the Library");
                    }
                    else 
                    {
                        Console.WriteLine("Sorry something went wrong");
                    }


                        //insert code here for finding a book and

                        //printing its details

                    break;

                case 'D':


                        //insert code here for displaying all of the books in the library        

                        // alphabetically by title

                    break;

                case 'E':

                        //insert code here for displaying all of the books in the library        

                        // alphabetically by author

                    break;

                case 'F':

                        //insert code here for displaying all of the books in the library        

                        // in ascending order by id num

                    break;



                case 'R':

                    Console.WriteLine ("Select a Book by ID to Remove");
                    int removeid = int.Parse (Console.ReadLine ());




                    break;

                default:

                    Console.WriteLine ("\nInvalid choice, please re-enter");

                    break;

                }

                printMenu ();

                p = Console.ReadLine ();

                p = p.ToUpper ();

                pick = p [0];

            }



            Console.WriteLine ("good bye");



        }



        public static void printMenu ()
        {

            Console.WriteLine ("\nSelect one of the following:\n\n" +

            " A  to add a book to the library\n" +

            " S  to search for a book by title\n" +

            " D  to display the contents of the library, alphabetically  by title \n" +

            " E  to display the contents of the library, alphabetically  by author \n" +

            " F  to display the contents of the library, in ascending order by id num \n" +

            " R  to remove a book from the library\n" +

            " Q  to quit this program\n\n");

            Console.Write ("enter choice here: ");



        }

    }

0 个答案:

没有答案