How to increment a string variable c#

Asked

Viewed 685 times

0

I have been developing a new application I really need to increase the variable

public string malcoins = "0";

good to begin I will not be able to put the code all because it is very big and here accept only to a certain number of characters I tried before without success. good here in this part of the code I need to increment the variable and then refresh the variable which is supposedly a type of score given to the user or withdrawal here varialvel is put in a label

label2.Text = malcoins;

and here when the user clicks on the

private void button1_Click(object sender, EventArgs e)
    {
        Clipboard.SetText(messageTextBox.SelectedText);
        Process.Start(Clipboard.GetText());

        malcoins = Convert.ToInt32(malcoins) + 100;
        deletemenssage();

    }

I tried so with the code assima but without success of eleminar error. Error 1 Cannot implicitly Convert type 'int' to 'string'
Testform.Cs 961 24

  • The variable malcoins was declared as string, you’re trying to assign an integer at the click of the button.

  • Malcoins needs to really be a string ?

  • not actually can’t be an int but I don’t know how to put the int value later on label.text = malcoins and always give me that error

  • If you are running calculations where the results are always integers it is interesting that malcoins are of type int and not string. To assign in label.Text only use malcoin.Tostring();

1 answer

2


Option 1

Doing it right and changing the type of the variable malcoins

public int malcoins = 0;

In the click event

private void button1_Click(object sender, EventArgs e)
{
    Clipboard.SetText(messageTextBox.SelectedText);
    Process.Start(Clipboard.GetText());

    malcoins = malcoins + 100;
    deletemenssage();
}

And change the time to show on label

label2.Text = malcoins.ToString();

Option 2

Convert the calculation result to string to put into the variable malcoins

private void button1_Click(object sender, EventArgs e)
{
    Clipboard.SetText(messageTextBox.SelectedText);
    Process.Start(Clipboard.GetText());

    malcoins = (Convert.ToInt32(malcoins) + 100).ToString();
    deletemenssage();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.