2014-01-12 39 views
0

我跑我的C#应用​​程序,它是很大的,所以我只是包含的代码布尔在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'

+1

什么是'dRow'类型的其他地方做了Converter类? 'interactionType'的类型是什么?以及哪条线路导致错误? – Liel

+2

您似乎正在使用[此课程](https://github.com/Gnuns/Bfly/blob/master/Butterfly%20Emulator/HabboHotel/Items/InteractionType.cs)。尽管下面的大多数答案都是善意的,但它们将不起作用,因为“InteractionType”没有从“True”或“False”转换。根据您提供给我们的信息,我很抱歉地说您的问题无法回答。 –

+0

查看'InteractionType'枚举(其中包括诸如“football”,“teleport”和“firegate”之类的值),并确保传递给'GetTypeFromString'的值在那里。 –

回答

1

取而代之:

(string)dRow[16] 

尝试ToString

dRow[16].ToString() 

或者Convert.ToString

Convert.ToString(dRow[16]) 
1

改变这一行:

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

这样:

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

解析代码中的未知交互类型:False System.InvalidCastException:指定的投射无效 – Fernando

+0

请查看@Michael Petrotta对您的问题的评论。这个错误与您的问题无关,这是如何将布尔转换为字符串。这个错误是由于使用了您正在使用的'InterractionTypes'库,它不接受'False'作为有效的输入值。 – Liel

0

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

+0

解析代码中的未知交互类型:False System.InvalidCastException:指定的强制转换无效 – Fernando

1

一个bool不能直接转换为需要转换一个string。你可以用与您在代码

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