1

I'm trying to display a Button called track with the text: "Start". But when I click the button I want to change the text to "Stop". Well, that's not much of a problem, but I want to change the text back in the "Start" when I press the button again. And over and over.

I already tried with an if-statement and using null, but that doesn't work. Also, the options when pressing alt+enter do not seem to work.

protected void change(object sender, EventArgs ea)
{
    if (track.Text == "Track")
{
    track.Text = "Track";
}
else
{
    track.Text = "Stop";
}
}
inge
  • 19
  • 4
  • 1
    You need the comparison operator (`==`), which returns a `bool`, rather than the assignment operator (`=`), which returns the result of the assignment (a `string` in this case). – Rufus L Dec 19 '18 at 21:39

3 Answers3

3

Should be if (track.Text == "Start"), not single =.

Tigran
  • 61,654
  • 8
  • 86
  • 123
2

You are attempting to do exactly what the errors says. Instead of if (track.Text = "Start" , it should be if (track.Text == "Start") :

protected void changetext(object sender, EventArgs e)
{
    if (track.Text == "Start")
    {
        track.Text = "Start";
        status.Text = "Je staat stil";
    }
    else
    {
        track.Text = "Stop";
        status.Text = "Je bent in beweging. Ga zo door!";
    }
}

After OP's edit:

protected void change(object sender, EventArgs ea)
{
    if (track.Text == "Track")
    {
        track.Text = "Track";
    }
    else
    {
        track.Text = "Stop";
    }
}
Jaskier
  • 1,075
  • 1
  • 10
  • 33
0

In other languages the test if (track.Text = "Start") is nearly always a typo.

C# only allows bool for comparison, to virtually eliminate the risk of a typo leading to a bug.

The only possible mistake remaining is if you have something like this :

Bool a = true;
Bool b = false;
If(a = b){
  //this is true
}

Next time pay attention to that, you wan’t to compare two values, not the result of an affectation.

Faure Ugo
  • 69
  • 2
  • 12