0

Ive searched, maybe not long enough but search nonetheless, and couldn't find a object.property error like mine.

"An object reference is required for the non-static field, method, or property"

I couldnt find the fix. been almost 3 hours. Im using a linear search and searching for a match between book title and a search input. assuming Book.Title would work, it throws back an error.

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;
        }
    }
} 

Book Class

{
       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;
        }
    }
}

Main Class

    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: ");



        }

    }
D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • In `if (search == Book.Title)` you are referring to Book statically. You are intending to write `if (search == books[i].Title)` – npearson Feb 01 '18 at 03:42
  • You should have used `foreach`. But for now you should do `if (search == books[i].Title)` – Chetan Feb 01 '18 at 03:42
  • @ChetanRanpariya He is returning the books index so `foreach` would not help. Perhaps `FindBook` should return a `Book`, in which case your suggestion is better. – npearson Feb 01 '18 at 03:44

0 Answers0