2

I am trying to compare if string that user inputs exists in list and if it does I would like to remove it from list.

I am getting an Error:

Argument 1: cannot convert from 'project.MainClass.Artical' to 'string'

I am aware that I am getting it because I am trying to compare string with list.

I have struct:

struct Artical {
  public string name;
  public float price;
  public double weight;
}

And method:

static void DeleteArtical()
{
    Artical k = new Artical();

    Console.WriteLine("Enter name you want to delete: ");
    k.name = Console.ReadLine();

    List < Artical > articals = new List < Artical > ();
    using(StreamWriter sw = File.AppendText((@ "../../dat.txt"))) {
      if (articals.Exists(x => string.Equals(x, k.name, StringComparison.OrdinalIgnoreCase))) { 
        Console.WriteLine("Done !!");
        artikli.Remove(k);
      }

    }
}

x is underlined and gives me error that I mentioned above.

1
  • you compare Artical object to a string value. change it to => x.name, k.name Commented Feb 12, 2018 at 13:58

2 Answers 2

3

The error comes from the point that you are trying to comare an object of type Artical with a string. The x in your lambda expression represents one element from the List<Artical> so you need to access the property name with it, because this is what you want to compare:

if (articals.Exists(x => string.Equals(x.name, k.name, StringComparison.OrdinalIgnoreCase)))

Please note, that Remove :

Removes the first occurrence of a specific object from the List

Furthermore you need to make sure that all other values (price and weight) in the compare object have to match the one that you are trying to find, otherwise the item will not be removed!

EDIT:

what you could do is to pull out actually the struct that you are looking for using FirstOrDefault.

This method:

Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.

The default value would be an empty struct with default values for each property. Knowing this you can then check for success of your finding procedure by checking wether name has a value at all. If you have successfully found the item you can now use the very same object to remove it from the list:

Artical objectForRemoval = articals.FirstOrDefault(x => string.Equals(x.name, k.name, StringComparison.OrdinalIgnoreCase));

if (!String.IsNullOrWhiteSpace(objectForRemoval.name))
{
    Console.WriteLine("Done !!");
    articals.Remove(objectForRemoval);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I don't have error any more but my object is not removed from list. Do you know why?
Great! Thank you for your answer!
@user9347049 I added a different way of how to delete the object. Have a look on my edit
1

If you need to take the string the do not need to get it into the Article object. Instead use simple string.

And, in your code you are comparing the whole object with string so it throws the error. Change the x with x.name

static void DeleteArtical()
{
    string input;

    Console.WriteLine("Enter name you want to delete: ");
    input = Console.ReadLine();

    List < Artical > articals = new List < Artical > ();
    using(StreamWriter sw = File.AppendText((@ "../../dat.txt"))) {
      if (articals.Exists(x => string.Equals(x.name, input, StringComparison.OrdinalIgnoreCase))) { 
        Console.WriteLine("Done !!");
        artikli.Remove(k);
      }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.