Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have done this calculator, and I have passed the arguments to the method and the parse have been done, however, is still having a wavy red line under
calculator.DoSomeMath(num1, num2)
and when hovered over that area it displays "
Cannot implicitly convert type int to string

Any suggestions? Thanks

public partial class MainPage : ContentPage
   {
       public MainPage()
       {
           InitializeComponent();

       }
       private void Button_Click(object sender, EventArgs e)
       {

           int num1 = int.Parse(number1.Text);

           int num2 = int.Parse(number2.Text);

           label3.Text = calculator.DoSomeMath(num1, num2);


       }

   }

       class calculator
       {

       public static int DoSomeMath(int num1, int num2)
       {

           int sum;
           sum = num1 + num2;
           return sum;

        }

   }


What I have tried:

I have tried
-
DoSomeMath.ToString(num1, num2);

-
label3.Text = calculator.DoSomeMath.(num1.ToString, num2.ToString);

-
label3.Text = calculator.int.Parse(DoSomeMath.(num1.ToString, num2.ToString));

-
int num1 = Convert.ToInt32(number1.Text);

int num2 = Convert.ToInt32(number2.Text);
Posted
Updated 10-Dec-21 20:00pm
v2

C#
// convert to string:
label3.Text = calculator.DoSomeMath(num1, num2).ToString();
Use 'TryParse to make sure the input values are valid:
C#
int value1, int value2;

if(! Int32.TryParse(num1, out value1))
{ 
     Console.WriteLine($"cannot convert {num1} to number");
     return;
}

// repeat test for 'num2
 
Share this answer
 
v2
Comments
Diasalva5 11-Dec-21 11:34am    
Dear Mr.BillWoodruff
It worked, Thanks. I had done something similar, but the one that you advised me was the one that worked.
THANK YOU!!
To expand on what Bill has - rightly - said ...
C# is a "strongly typed" language, which means that it assumes you know what you are doing, and it doesn't - so it won't make an assumption about what you want and convert data from type to type for you, unless that conversion would result in a very similar type without any loss of information.

So it will happily "upgrade" an integer to a float or double:
C#
int x = 666;
float y = x;
C#
int x = 666;
double y = x;
because both types are numeric and there is no loss of data.But it won't convert the other way:
C#
float x = 666.0f;
int y = x;
C#
double x = 666.0;
int y = x;
because integers can't store fractional parts of a number.
To do that, you have to explicitly tell the system what you want to do using a cast operation:
C#
float x = 666.0f;
int y = (int) x;
C#
double x = 666.0;
int y = (int) x;

And the same applies when you try to do this:
C#
int x = 666;
string y = x;
will fail because the two types are too different and you can't cast between them.

To assign a numeric value to a string, you have to explicitly tell the system to do the conversion:
C#
int x = 666;
string y = x.ToString();
And the ToString method can take a parameter which tells it how to format the number when you convert it: UInt32.ToString Method (System) | Microsoft Docs[^]
 
Share this answer
 
Comments
Diasalva5 11-Dec-21 11:37am    
Dear OriginalGriff:
I have studied on some books this "convert-try-parse" issue, but your explanation has been (at least for me) one of the best ones. Your statements have clarified many of my questions. THANK YOU!
OriginalGriff 11-Dec-21 11:56am    
You're wlecome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900