我正在嘗試從 C# 中的串列框資料計算中值。但是,在 foreach 陳述句中發生無效轉換例外,如下所示(粗體):
foreach(ListBox1.Items 中的字串項)
誰能幫我解決這個錯誤?將不勝感激。
謝謝你。
System.InvalidCastException:無法將“System.Int32”型別的物件轉換為“System.String”型別
private void calcButton_Click(object sender, EventArgs e)
{
medianList.Clear();
ListBox2.Items.Clear();
if (sourceList.Count == 0)
{
double tInt;
foreach (string item in ListBox1.Items)
{
if (double.TryParse(item, out tInt) == true)
sourceList.Add(tInt);
}
}
if (ListBox1.Items.Count > 0)
{
if (RadioButton1.Checked)
MiddleMedian();
else
AllMedian();
}
else
return;
DisplayResults();
sourceList.Clear();
}
uj5u.com熱心網友回復:
那么,錯誤訊息是否有意義?
無法將“System.Int32”型別的物件轉換為“System.String”型別
foreach (string item in ListBox1.Items)
因此,您已將整數添加到ListBox1. 然后修復它:
foreach (int tInt in ListBox1.Items)
sourceList.Add(tInt);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/429161.html
