0

I need to find the klas of 5TIF2 and dont now how to compare it to the class

private void BtnFind_Click(object sender, RoutedEventArgs e)
{
    Leerling oleerling = new Leerling();
    oleerling.MijnId = TxtId.Text;
    oleerling.Voornaam = TxtVoornaam.Text;
    oleerling.Naam = TxtNaam.Text;
    oleerling.Klas = TxtKlas.Text;

    if (oleerling.Klas = "5TIF2")
    {
        LstLeerlingen.Items.RemoveAt(2);
        LstLeerlingen.Items.RemoveAt(3);
    }
    else
    {
        LstLeerlingen.Items.RemoveAt(0);
        LstLeerlingen.Items.RemoveAt(1);
    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Jacy
  • 9
  • 1
  • 7
    Use `==` equality operator in `if` statement, `=` is used to assign value. – Yong Shun May 27 '21 at 09:27
  • 1
    The question seem to really be, "how to compare strings", since `oleerling.Klass` and `"5TIF2"` are probably both strings. See programming guide: [How to compare strings](https://learn.microsoft.com/en-us/dotnet/csharp/how-to/compare-strings) – JonasH May 27 '21 at 09:30
  • 2
    `==` is usually fine, but if you need more control over the exact rules for the comparison then use `.Equals()` - see https://learn.microsoft.com/en-us/dotnet/api/system.string.equals?view=net-5.0 for details – ADyson May 27 '21 at 09:31
  • Does this answer your question? [Differences in string compare methods in C#](https://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp) –  May 27 '21 at 09:36

1 Answers1

3

You need to use the operator == to check if a statement is true or false, instead you have used =, which is for assigning values.

You need:

  if (oleerling.Klas == "5TIF2")
R_Dax
  • 706
  • 3
  • 10
  • 25