How to know the data type of value entered by user at runtime in textbox?

徘徊边缘 提交于 2019-12-13 02:22:45

问题


How to know the data type of value entered by user at runtime in textbox?

My simple example:

I've tried it by using GetType(), but it was useless, it always shows System.String, whether I enter int or String.


回答1:


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.



来源:https://stackoverflow.com/questions/16294589/how-to-know-the-data-type-of-value-entered-by-user-at-runtime-in-textbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!