0

I am trying to make a like and a dislike button. The like button works perfectly but when the dislike button is clicked it doesnt do anything and when it is clicked again it removes a like. What can I do to fix this problem; I have a like button(iconButton1) a dislike button(iconButton1) and a label(label1).

        int i;
        int like;
        int dislike;

        private void iconButton1_Click(object sender, EventArgs e)
        {
            like = i ++ ;
            label1.Text = like.ToString();        
        }

        private void iconButton2_Click(object sender, EventArgs e)
        {
            dislike = like -- ;
            label1.Text = dislike.ToString();
            
        }
Ody
  • 1
  • 1
  • 3
    The logic isn't clear. What is `i` for? When they hit "like" should "dislike" decrease and vice versa? – 001 Sep 08 '22 at 19:17
  • 2
    Why not just use `like++` and `dislike--` – jbutler483 Sep 08 '22 at 19:29
  • I fixed the code with your advise and just used for the like btt like++ and for the dislike ``dislike=like-- – Ody Sep 08 '22 at 20:06
  • It's not clear to me what the `like` and `dislike` variables are supposed to represent. What do their values mean? (You posted code that has a logic failure, but haven't described the logic it's supposed to represent) – Rufus L Sep 09 '22 at 00:19

1 Answers1

0

It sounds like you're keeping track of the number of times a button is clicked. If this is the case, then you don't need i at all - just variables that represent the "Like" and "Dislike" buttons:

int like;
int dislike;

private void iconButton1_Click(object sender, EventArgs e)
{
    // Pre-increment 'like' and display it's value
    label1.Text = ++like.ToString();        
}

private void iconButton2_Click(object sender, EventArgs e)
{
    // Pre-increment 'dislike' and display it's value
    label1.Text = ++dislike.ToString();
}

Notice there's a subtle difference between a pre-increment (++variable) and post-increment (variable++). The pre-increment will use the incremented value in the expression, and the post-increment will use the non-incremented value in the expression.

Eric Lippert describes it much better here: What is the difference between i++ and ++i?

Rufus L
  • 36,127
  • 5
  • 30
  • 43