Bool要在C#中串起来?

时间:2014-01-12 21:31:40

标签: c# string boolean

我运行的C#应用​​程序很大,所以我只是包含了部分代码

我的代码:

allowGift = Convert.ToInt32(dRow[14]) == 1;
allowInventoryStack = Convert.ToInt32(dRow[15]) == 1;
interactionType = InterractionTypes.GetTypeFromString((string)dRow[16]); //Line of error

堆栈跟踪错误

  

System.InvalidCastException:无法将typpe'System.Boolean'的对象强制转换为'System.String'

4 个答案:

答案 0 :(得分:2)

而不是:

(string)dRow[16]

尝试ToString

dRow[16].ToString()

Convert.ToString

Convert.ToString(dRow[16])

答案 1 :(得分:1)

更改此行:

interactionType = InterractionTypes.GetTypeFromString((string)dRow[16]); //Line of error

到此:

interactionType = InterractionTypes.GetTypeFromString(dRow[16].ToString()); //Line of error

答案 2 :(得分:1)

bool无法直接转换为需要转换的string。您可以像在代码中的其他地方一样使用Converter

InterractionTypes.GetTypeFromString(Convert.ToString(dRow[16]));

答案 3 :(得分:0)

而不是(string)dRow[16]使用dRow[16].ToString()