简体   繁体   中英

Like/ Dislike button not working properly

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();
            
        }

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM