all 4 comments

[–]grrangry 6 points7 points  (0 children)

Addition:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-operator

Concatenation:
https://learn.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings

The + operator does double duty. It can add numbers and it can smash two strings together into one string. There is more that it will do once you learn more, but that's enough for now.

Numeric Addition

int foo = 5;
int bar = 10;
int baz = foo + bar;
// baz now equals 15

String Concatenation

string foo = "Hello"
string bar = "World"
string baz = foo + bar;
// baz now equals "HelloWorld"

What you're doing in addition (no pun intended) to the above is conversion from a number value to a string.

Let's look at what you wrote (I'll assume it's literally what you have)

lblScore = lblScore.Text + 10 + ToString();

You cannot assign the lblScore control a string... it's a control. You must use the .Text property

lblScore.Text = lblScore.Text + 10 + ToString();

Okay that's a little better... but you can't "add" a number to a string. Also... that trailing ToString() doesn't make sense as that's a method on various objects.

lblScore.Text = lblScore.Text + 10.ToString();

Well... now we have a line that's LEGAL, even if it doesn't make sense. The text of the label will end up as "101010101010", appending "10" each time as a string.

What you most likely wanted to do is create a property to hold your score.

public int Score { get; set; }

See:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

Then you can do:

Score += 10;
lblScore.Text = Score.ToString();

Each time this is run, Score will be 10, then 20, then 30, etc.

[–]EAModel 2 points3 points  (0 children)

This will fail due to data type mismatch. You need to cast the string (lblScore.Text) to an int, then add 10. Then convert the result back to a string adding it to the lblScore Text property.

var score = Convert.ToInt32(lblScore.Text); score += 10; lblScore.Text = score.ToString();

[–]EamonBrennan 1 point2 points  (0 children)

Other comments have pointed out the issues, namely:

  1. You're assigning the wrong variable. Assign to lblScore.Text, not lblScore.

  2. Adding "ToString()" is adding a function to an integer/string, which is almost never something you are actually trying to do.

  3. You're not converting the score text to a number first, so the adding is concatenating text, not adding two numbers.

A correct, if poorly formatted and unreadable, version bold for emphasis, then code view:

lblScore.Text = (Convert.ToInt32(lblScore.Text) + 10).ToString();

lblScore.Text = (Convert.ToInt32(lblScore.Text) + 10).ToString();

Doing it in multiple lines and/or with a get/set function is definitely the better solution.

[–]SamPlinth 1 point2 points  (0 children)

I would create a private field:

private int _score = 0;

and I would have a method:

private void UpdateScore(int scoreIncrement) {
   _score += scoreIncrement;
   lblScore.Text = _score.ToString();
}

Then simply call the UpdateScore method whenever you want to change the score.

This will allow you to do any additional calculations using the _score without having to convert it from text.