Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

C#

2»

Comments

  • Administrators Posts: 53,335 Admin ✭✭✭✭✭awec


    Would you mind showing me an example of what you mean?

    If the first book in your collection doesn't meet your search criteria, the function will return false, and therefore won't check any subsequent books.


  • Registered Users Posts: 14,715 ✭✭✭✭Earthhorse


    Would you mind showing me an example of what you mean?

    What he means is:
    public Boolean CheckIfBookExists(string name,string title)
    {
         bool bookExists = false;
         foreach(Library lib in library)
         ...
    }
    

    Then use that bool as your return value. I won't write the entire function for you because you should really do it yourself so you internalise it.

    If you can't understand why your existing for loop, or indeed any subsequent ones you write, isn't working as expected I strongly encourage you to use the debugger and step through each line of code to see what's happening. Visual Studio has really great runtime debugging tools and you should use them.


  • Registered Users Posts: 1,547 ✭✭✭rock22


    Would you mind showing me an example of what you mean?

    Sorry , didn't see your post but both Awec and Earthhorse have explained what I meant.

    You should try to complete the code Earthhorse provided.


  • Administrators Posts: 53,335 Admin ✭✭✭✭✭awec


    This is the most optimum fix if you want to keep using linear search. No point in continuing to enumerate once you find what you're looking for.
    public Boolean CheckIfBookExists(string name,string title)
            {
                foreach(Library lib in library)
                {
                    if(lib.AuthorName.Equals(name) && lib.BookName.Equals(title))
                    {
                        return true;
                    }
                    [S]else
                    {
                        return false;
                    }[/S]
                }
                return [S]true[/S] false;
            }
    

    Linear search is not a great approach to take if your collection can be large, there are much more performant approaches. If you think about it practically, if you were searching for a specific book in a library you wouldn't start at one end of the room and go book-by-book to the other end to try find it.


  • Registered Users Posts: 4,832 ✭✭✭shootermacg


    You don't really even need a method:

    return Library.Any(lib =>
    lib.AuthorName.Equals(name) && lib.BookName.Equals(title));


  • Advertisement
  • Registered Users Posts: 14,715 ✭✭✭✭Earthhorse


    awec wrote: »
    This is the most optimum fix if you want to keep using linear search.

    Most optimum is not the most optimum way to say optimum.

    Anyway, the most optimum fix is to fix the coder, not the code.

    :cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool:


Advertisement