如何知道用户在运行时在文本框中输入的值的数据类型? [英] How to know the data type of value entered by user at runtime in textbox?
问题描述
如何知道用户在运行时在文本框中输入的值的数据类型?
How to know the data type of value entered by user at runtime in textbox?
我的简单示例:
我已经使用GetType()
进行了尝试,但是它没有用,无论我输入int
还是String
,它始终显示System.String
.
I've tried it by using GetType()
, but it was useless, it always shows System.String
, whether I enter int
or String
.
推荐答案
如果用户在文本框中输入了文本,则该文本总是 一个字符串.它从来不是一个整数.您可以将解析为整数,但是输入本身仍然是文本.
If the user has typed text into a textbox, that's always a string. It's never an int. You can parse the text as an integer, but the input itself is still text.
您可以推测性地尝试以不同的方式对其进行解析:
You could speculatively try to parse it in different ways:
int intValue;
if (int.TryParse(text, out intValue)
{
... use intValue, then return?
}
decimal decimalValue;
if (decimal.TryParse(text, out decimalValue)
{
... use decimalValue, then return?
}
但是从根本上讲,您需要了解用户输入总是 个字符串,并且如何使用该字符串取决于您.
But fundamentally you need to understand that the user input is always a string, and how you use that string is up to you.
这篇关于如何知道用户在运行时在文本框中输入的值的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!