无法将类型'int'隐式转换为'string'

无法将类型'int'隐式转换为'string'

问题描述:

我正在编写Listbox的代码,用于计算ListBox中当前的总数值的总和。
这样的
i geting错误 - 不能隐式地将类型'int'转换为'string'

错误显示此行 - TotalFees.Text =总计;

我的ListBox值是这种类型

500

1000

1500

2000





帮助我任何人



我尝试过:



I am write the code for Listbox for calculating the sum of Total Numeric Values in present in ListBox.
i geting error like this-Cannot implicitly convert type 'int' to 'string'
Error shows here this line- TotalFees.Text = Total;
My ListBox values are this type
500
1000
1500
2000


help me anyone

What I have tried:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       int Total;
       foreach (string Str in ListBox1.Items)
       {
           Total = (Total + int.Parse(Str));
       }

       TotalFees.Text = Total;
   }

TotalFees.Text = Total.ToString();


解决这个问题很容易:

It is quite easy to solve this:
TotalFees.Text = Total.ToString();


int Total = 0; // set Total to zero to start
foreach (string Str in ListBox1.Items)
{
    Total = (Total + int.Parse(Str));
}



首次使用前需要初始化Total。只需修改上面的代码。


You need to initialise Total before using it the first time. Just modify the code as above.