Determining the Datatype from the typed text in text box
-
Hi, I am working on a WPF application with .Net Framwork 3.5. On my application Window I have a free text box. Is there any class in .Net which helps in determining the datatype of the typed text in the text box? Eg. If user types "Vipul1234" OR "Vipul" then the class should determine the data typed in the textbox is of type String If user types "1234" then the class should determine the data typed in the textbox is of type int
Regards, Vipul Mehta
-
Hi, I am working on a WPF application with .Net Framwork 3.5. On my application Window I have a free text box. Is there any class in .Net which helps in determining the datatype of the typed text in the text box? Eg. If user types "Vipul1234" OR "Vipul" then the class should determine the data typed in the textbox is of type String If user types "1234" then the class should determine the data typed in the textbox is of type int
Regards, Vipul Mehta
Hi, what you type in a TextBox is text, hence a string. it may also represent something else, such as an int, a double, a DateTime, etc. In order to check, you should call TryParse on each relevant type, and be careful about the order (so an integer may turn into either an int or a double). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hi, what you type in a TextBox is text, hence a string. it may also represent something else, such as an int, a double, a DateTime, etc. In order to check, you should call TryParse on each relevant type, and be careful about the order (so an integer may turn into either an int or a double). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Hi Luc, As the value in the textbox would always be text i.e. of string data type. So how tryparse will determine of its actual type as int or double or some other? Shilpa
-
Hi Luc, As the value in the textbox would always be text i.e. of string data type. So how tryparse will determine of its actual type as int or double or some other? Shilpa
Well, TryParse, whatever the base class, will try and parse the string; it will fail when the data does not fit the type you are trying to parse, so a sequence of different TryParse calls should be sufficient to determine the type. Check the documentation on int32.TryParse, DateTime.TryParse :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hi, I am working on a WPF application with .Net Framwork 3.5. On my application Window I have a free text box. Is there any class in .Net which helps in determining the datatype of the typed text in the text box? Eg. If user types "Vipul1234" OR "Vipul" then the class should determine the data typed in the textbox is of type String If user types "1234" then the class should determine the data typed in the textbox is of type int
Regards, Vipul Mehta
Wouldn't it make more sense to use the textbox as it was intended to be used - to capture text - then cast the input to whatever type you need? Even if you're re-using a control for different types, your application should "know" what it's expecting at each point. If that isn't the case, you can try casting the input to various types and catching the exceptions thrown to determine the type. Even so, you're going to get misleading results if, for example, you ask for a password and the user chooses '123456' as a password. Better, I think, to clearly define what you expect at each input opportunity and constrain the input to compatible types.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
-
Hi, what you type in a TextBox is text, hence a string. it may also represent something else, such as an int, a double, a DateTime, etc. In order to check, you should call TryParse on each relevant type, and be careful about the order (so an integer may turn into either an int or a double). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Thanks very much... This worked well
Regards, Vipul Mehta