Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Determining the Datatype from the typed text in text box

Determining the Datatype from the typed text in text box

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpwpfquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vipul Mehta
    wrote on last edited by
    #1

    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

    L R 2 Replies Last reply
    0
    • V 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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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!


      S V 2 Replies Last reply
      0
      • L Luc Pattyn

        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!


        S Offline
        S Offline
        ShilpaGinodiya
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • S ShilpaGinodiya

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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!


          1 Reply Last reply
          0
          • V 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

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #5

            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"

            1 Reply Last reply
            0
            • L Luc Pattyn

              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!


              V Offline
              V Offline
              Vipul Mehta
              wrote on last edited by
              #6

              Thanks very much... This worked well

              Regards, Vipul Mehta

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups