0

I am new to C# and just started to learn how to code. I was trying to convert and sum an amounts for several items displayed in a label, then displays the total in another label.I used parse to convert the values to double ,but I've frequently got an error message saying that"cannot implicitly convert type int to string.Here is a sample of my code.

int Cost;

double total = 0;
costLabel.Text = Convert.ToInt32(priceLabel2.Text);
Cost = int.Parse(priceLabel2.Text);
total += double.Parse(priceLabel2.Text);
costLabel.Text = total.ToString("c");

can any one help me to solve this problem?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
S.R
  • 27
  • 2
  • 4

5 Answers5

1

Please, mind types; your code amended:

 // Are you sure that price is integer? What about 4.95$? 
 // More natural choice is double (decimal is the best for the currency) 
 double Cost;

 // Let's preserve double (but decimal is a better choice) 
 double total = 0;

 // string assigned to string; no Convert.ToInt32  
 // It's useless line, however, since costLabel.Text = total.ToString("c");
 // will rewrite the label
 costLabel.Text = priceLabel2.Text; 

 // Since cost is not an integer value
 Cost = double.Parse(priceLabel2.Text);

 // You don't want to parse twice
 total += Cost;
 costLabel.Text = total.ToString("c");

A better choice is to use decimal for currency:

 decimal total = 0m;

 //TODO: it seem's that you want to add some logic here; otherwise total == cost

 decimal cost = decimal.Parse(priceLabel2.Text);
 total += cost; 

 costLabel.Text = total.ToString("c");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    A better choice is `decimal`. – Patrick Hofman Jun 06 '17 at 07:36
  • I have edited my code, but I am still getting an error on the line : Cost=double.parse(priceLabel2.Text); The error message saying that :when converting a string to date/time, parse the string to take the date before putting each variable into the Date Time object. – S.R Jun 06 '17 at 07:52
  • @S.R: you have the error *somewhere else* in your code: "when converting a string to **date/time**, parse the string to take the date before putting each variable into the **Date Time** object" - we don't have any `DateTime` in the fragment – Dmitry Bychenko Jun 06 '17 at 07:54
0

The problem is in the line costLabel.Text = Convert.ToInt32(priceLabel2.Text);. You have a lot of unnecessary code, so it could all be simplified to the following:

double total = double.Parse(priceLabel2.Text);
costLabel.Text = total.ToString("c");
Wayne Allen
  • 1,605
  • 1
  • 10
  • 16
0

You are assigning integers to strings, that won't work. You are also performing multiple operations on the same input. For currencies, you must use decimal since that is a safer (more precise) data type.

decimal total = 0;
...

decimal price = Convert.ToDecimal(priceLabel2.Text);
total += price;

costLabel.Text = total.ToString("c");

If you want to validate the input first, you should use decimal.TryParse:

if (decimal.TryParse(priceLabel2.Text, out decimal price))
{
    total += price;
}
else
{
    MessageBox.Show("Input is not a valid decimal.");
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I tried the above mentioned code, but I am still getting an error on the line : decimal price = Convert.ToDecimal(priceLabel2.Text); The error message saying that :when converting a string to date/time, parse the string to take the date before putting each variable into the Date Time object – S.R Jun 06 '17 at 08:02
  • You can try me now, but I think Dmitry was very clear. There is no date/time in your sample code. Our code will not give that error, I am sure. – Patrick Hofman Jun 06 '17 at 08:03
0

costLabel.Text property type is a string.

costLabel.Text = priceLabel2.Text;
Daniel Tshuva
  • 483
  • 4
  • 12
0

costlLabel.Text is a string and you try to give it an integer value:

costLabel.Text = Convert.ToInt32(priceLabel2.Text);

use this instead:

costLabel.Text = priceLabel2.Text;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325