【发布时间】:2018-11-08 02:56:07
【问题描述】:
我正在尝试使用Microsoft.VisualBasic.dll 从用户那里获取输入。然后将输入从 [string] 转换为 [char] 类型。但是当我运行代码时它给了我这个错误:
*An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
String must be exactly one character long.*
示例代码:
string directions = Microsoft.VisualBasic.Interaction.InputBox("1 = Buy, 2 = Sell", "Select side", "Default", 700, 400);
char direction = System.Convert.ToChar(directions);
知道如何解决这个问题吗?提前致谢。
【问题讨论】:
-
当然,我知道这个“字符串必须恰好是一个字符长”是什么意思。对于
directions,我没有设置长度。但是,经过几次尝试,我发现任何超过 1 个值的输入都会出现此错误。相反,只有输入 1 个值(字母数字)才能正常工作。 -
记住一个字符是单个字符,而字符串是0个或多个字符串在一起。
-
你没有设置长度,当然。但是你设置了一个 specific 长度的值,不是吗?我认为 1 次尝试足以确定字符串“12”的长度为 2。
标签: c# .net string char inputbox