GuinnessIsLife February 2016

How to calculate a score when you drag and drop information from a textbox into multiple DrapDrop event

I am creating a simple game application in windows forms. You can drag the information from a textbox and drop it into a Dragdrop event from one MDI application to another. This is all OK but the problem I am having is that I have multiple DragDrop events and I am trying to calculate the score for all of them.

What I have tried so far is this.

public void score(int calcScore)
{
    lblScore.Text += calcScore.ToString();
}

private void square_DragDrop(object sender, DragEventArgs e)
{
    if(shape.Contains("Square"))
    {
        int calcScore = 0;
        calcScore++;
        score(calcScore);
    }
}    

private void circle_DragDrop(object sender, DragEventArgs e)
{
    if(shape.Contains("Circle"))
    {
        int calcScore = 0;
        calcScore++;
        score(calcScore);
    }
}    

The problem is that this will add the to score together as a string so the result will be 11 instead of 2.

If I try this:

public void score(int calcScore)
{
    int totalScore =0;
    totalScore = totalScore + calcScore;
    lblScore.Text += totalScore.ToString();
}

The problem with this is that once I initialize the totalScore it sets the total score back to 0 so the totalScore will always be 1.

How can I get the score properly calculated like this?

Answers


Ms Yvette ǝʇʇǝʌʎ sW February 2016

1.

public void score(int calcScore)
{
    lblScore.Text += calcScore.ToString();
}

This does a cocatenation of the strings. So:

"1" + "1" = "11";

Just as

"cat"+"cat" = "catcat";

2.

Initialise

int totalScore =0; 
int calcScore =0;

at a class level. Every time you use your DragDrop or method score(int calcScore) you are resetting these values to zero.

Post Status

Asked in February 2016
Viewed 3,722 times
Voted 11
Answered 1 times

Search




Leave an answer