Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type 'int' to 'string' C# class

Tags:

c#

I'm trying make a program where you call operations like addition from classes.

I don't know why there's an error:

public string Add() {
    string AdditionTotal;
    int num1 = int.Parse(txtFirstNumber.Text);
    int num2 = int.Parse(txtSecondNumber.Text);
    AdditionTotal = num1 + num2; //throws an error here           
    return AdditionTotal;
}

public string SetText {
    get {
        return txtFirstNumber.Text;
    }
    set {
        txtFirstNumber.Text = value;
    }
}
like image 646
user5207499 Avatar asked Sep 13 '15 02:09

user5207499


1 Answers

Try like this

AdditionTotal = (num1 + num2).ToString(); 

num1 and num2 both is an int and their sum is also an int

C# can't convert it directly from int to string .

you have to cast it pragmatically in order to assign.

like image 76
Anik Islam Abhi Avatar answered Sep 30 '22 19:09

Anik Islam Abhi