Home > OS >  Method is only returning null. Searchs class list if user input equals what the user has inputed ret
Method is only returning null. Searchs class list if user input equals what the user has inputed ret

Time:10-14

as said in the title I have a class list on everytime that the program lauches it reads a txt file and the info the txt file gets changed into objects of that class (this works without a issue), but when I want to verify that what a user has input is equaled to something in this class list it doesn't seem to return the proper value but instead only every returns null. I can't seem to figure out why, I suppose I could maybe change the class list to a string list and verify via that way but kinda seems not the proper thing to do. I will provide code below. Some ideas would be helpful!

clientaccount is meant to be the input, clientaccounts is the class list. If it doesn't contain it return null.

  public Client? GetAccountAccountsE(ClientEmail cliEmail)
    {

        foreach (Client clientaccount in clientaccounts)
        {
            
            if (clientaccount.Equals(cliEmail)) { return clientaccount; Console.WriteLine("returning account"); }
        }
        Console.WriteLine("nulling");
        return null;
    }

CodePudding user response:

You are comparing Client to ClientEmail, which are, unless you override the Equals()-method, not equal.

If ClientEmail email is a property of Client, try:

 if (clientaccount.email.Equals(cliEmail)) { 
   Console.WriteLine("returning account");
   return clientaccount;
 }
  • Related