Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Error (Cannot Implicitly convert type 'string' to 'int') [duplicate]

Tags:

c#

please help! I have not idea how to fix this.I am stuck on this and it has been a while. What should this code look like at lease the error is in line 8-10

        int intAmountA = 0;
        int intAmountB = 0;
        int intAmountC = 0;
        decimal decPackageA = 0m;
        decimal decPackageB = 0m;
        decimal decPackageC = 0m;
        decimal decTotal = 0m;

        intAmountA = TxtAmountA.Text;
        intAmountB = TxtAmountB.Text;
        intAmountC = TxtAmountC.Text;

        decPackageA = intAmountA * 150;
        decPackageB = intAmountB * 120;
        decPackageC = intAmountC * 90;

        LblPackageA.Text = decPackageA.ToString("c");
        LblPackageB.Text = decPackageB.ToString("c");
        LblPackageC.Text = decPackageC.ToString("c");

        decTotal = decPackageA + decPackageB + decPackageC;


        LblTotal.Text = decTotal.ToString("c");
like image 421
jimmy Avatar asked Dec 08 '25 00:12

jimmy


1 Answers

The TxtAmountA.Text is a string. You are attempting to set the variable intAmountA which is an integer to a string value, thus the error. You need to parse the integer out of the string in the textbox.

intAmountA = int.Parse(TxtAmountA.Text);

However, be aware that if what is in TxtAmountA.Text is not something that can be cast to an integer, you will get an exception. That is when you can use the conditional int.TryParse(string value, out integer);

like image 137
Tommy Avatar answered Dec 10 '25 14:12

Tommy