Home > Blockchain >  Why is this function not printing out the specified objects values? C#
Why is this function not printing out the specified objects values? C#

Time:10-22

Im creating a program that will be used like a library, where you can for exampel look up a spesific book.. Let me run you through the code..

The problem im trying to solve is that the lookup function does NOT print out the books in my library list that contain that letters in question

This class is used for creating the books in the library

    class Bok
    {
        public string titel;
        public string author;
        public bool loaned;

        public Bok(string titel, string author, bool loaned)
        {
            this.titel = titel;
            this.author = author;
            this.loaned = loaned;
        }
    }

And with this I have created a new list, called library along with an example books

            List<Bok> library = new List<Bok>() { };

            Bok book = new Bok("alex bok", "alexander", false);

Now I'm letting the user input a string, which will be used to search for a specific book located in that list, im making all the cases of the string answer to lower and then checking if any object in the list library has an object of which the values "author" or "title" contains the string that the user entered, and then prints it

            void Print(Bok bok)
            {
                Console.WriteLine("hej");
                Console.WriteLine($"{bok.titel}\n"  
                    $"{bok.author}\n"  
                    $"Lånad : {bok.loaned}");
            }

            void Lookup()
            {
                Console.WriteLine("Sök efter titel, eller författare");
                string svar = (Console.ReadLine()).ToLower();

                foreach(Bok bok in library)
                {
                    if (bok.author.Contains(svar) || bok.titel.Contains(svar))
                    {
                        Print(bok);
                    }
                    else
                    {
                        Console.WriteLine("Boken finns inte i biblioteket.");
                    }
                    
                }
            } 

CodePudding user response:

Since we are not seeing the whole code, I'm not sure if you already did this at some point of your code, but i would try to:

  1. Add the Bok book = new Bok("alex bok", "alexander", false); to the list List<Bok> library = new List<Bok>(); (get rid of the {} after creating the new list, you don't need them) with library.Add(book). Otherwise you iterate through an empty array in your Lookup() function.
  2. Did you call your Lookup() function at some point in the code? Otherwise its just defined, but not executed.

CodePudding user response:

I think what you are trying to do is add a collection of books then match the lower cased value.

Also consider using comparison or regex for more complex searches.

This however will work.

https://dotnetfiddle.net/nBswHI

  • Related