none
error outputting and search for items in list of objects RRS feed

  • Question


  • I have a bookshop class and a menu which gives the user different options. I am using a list to hold to objects. When I
    enter a new book from the menu then go to display the books nothing shows up and when I search for a books ISBN number I 
    get an error?

    I get the user to enter in isbn,author,title,price. I then instantiate the new object and add it to the list, I then try to display the
    contents of the list using a for each loop and try search for the isbn using a for loop but I receive an error. I cant see what i'm
    doing wrong.

    thanks.


    Friday, October 13, 2017 3:31 AM

All replies

  • Greetings Will.

    In the first place, never just say "I get an error". Always tell us what error, preferably by copying and pasting the exact text of the message.

    In the second place, don't post such long chunks of code as pictures. Always insert it using the button on the toolbar where you make your post.

    That gives us a chance to copy and past the code into a test program of our own to run and debug.

    Friday, October 13, 2017 4:21 AM
  • Hello will_mannix,

    In your "for" loop of case 3 , please change the condition as "i < books.Count".

    The maximum value of the list index is books.Count-1. when your assign the value of list index as books.Count it will throw OutOfRange exception.your code should be like this.

    for (int i = 0; i < books.Count; i++) {
                                if (isbn == books[i].ISBN) {
                                    Console.WriteLine("{0},{1}",i,books[i].ISBN);
                                }
                            }

    Sincerely,
    Neil Hu


    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.

    Friday, October 13, 2017 6:14 AM
    Moderator
  • Hello will_mannix,

    Is there anything updated? If the issues has been solved , you could close the thread by marking the reply  as answer, This can be beneficial to other community members reading this thread.

    Sincerely,
    Neil Hu


    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.

    Sunday, October 29, 2017 8:05 AM
    Moderator