Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C#

2»

Comments

  • Administrators Posts: 55,556 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, Registered Users 2 Posts: 14,724 ✭✭✭✭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, Registered Users 2 Posts: 1,862 ✭✭✭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: 55,556 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, Registered Users 2 Posts: 4,947 ✭✭✭shootermacg


    You don't really even need a method:

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


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 14,724 ✭✭✭✭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