如何知道文本框在运行时由用户输入的值的数据类型?(How to know the data type

2019-09-28 12:43发布

如何知道文本框在运行时由用户输入的值的数据类型?

我简单的例子:

我已经通过尝试过GetType()但它是无用的,它总是显示System.String ,我是否进入intString

Answer 1:

如果用户键入的文本到文本框,这总是一个字符串。 这是从来没有一个int。 您可以分析文本,作为一个整数,但输入本身仍是文本。

你可以推测尝试分析它以不同的方式:

int intValue;
if (int.TryParse(text, out intValue)
{
    ... use intValue, then return?
}

decimal decimalValue;
if (decimal.TryParse(text, out decimalValue)
{
    ... use decimalValue, then return?
}

但是从根本上,你需要理解用户输入始终是一个字符串,以及如何使用该字符串是你。



文章来源: How to know the data type of value entered by user at runtime in textbox?