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. C#
  4. How to get Text control value into a numeric data type?

How to get Text control value into a numeric data type?

Scheduled Pinned Locked Moved C#
helpquestiontutorial
6 Posts 3 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.
  • A Offline
    A Offline
    Amol Ravatale
    wrote on last edited by
    #1

    Hi All, I have a problem. I want to assign the value of text control to a variable of double type. If i do in this way double price = Text1.Text gives compilation error. If I do in this way double price = (double) Text1.Text gives me run time error of invalidcast type. How can I procceed in this case. Regards

    Every thing will come to you if you have faith.

    S L 2 Replies Last reply
    0
    • A Amol Ravatale

      Hi All, I have a problem. I want to assign the value of text control to a variable of double type. If i do in this way double price = Text1.Text gives compilation error. If I do in this way double price = (double) Text1.Text gives me run time error of invalidcast type. How can I procceed in this case. Regards

      Every thing will come to you if you have faith.

      S Offline
      S Offline
      Shy Agam
      wrote on last edited by
      #2

      Hello Amol Ravatale, Every value type (i.e. int, double, char, etc.) has a Parse() function. This functions are all static, meaning you don't need to instantiate an object of that type. In your case, do the following:

      double price = double.Parse(Text1.Text);
      

      Best regards, Shy. -- modified at 11:53 Sunday 30th July, 2006

      1 Reply Last reply
      0
      • A Amol Ravatale

        Hi All, I have a problem. I want to assign the value of text control to a variable of double type. If i do in this way double price = Text1.Text gives compilation error. If I do in this way double price = (double) Text1.Text gives me run time error of invalidcast type. How can I procceed in this case. Regards

        Every thing will come to you if you have faith.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Double.TryParse() is what you want.

        S 1 Reply Last reply
        0
        • L Lost User

          Double.TryParse() is what you want.

          S Offline
          S Offline
          Shy Agam
          wrote on last edited by
          #4

          You're right... It's better that way since you can't really tell what's in the box... :)

          L 1 Reply Last reply
          0
          • S Shy Agam

            You're right... It's better that way since you can't really tell what's in the box... :)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Well, you can always catch the exception in Parse :) Also, Parse is .NET 1.1, while TryParse is only .NET 2.0

            A 1 Reply Last reply
            0
            • L Lost User

              Well, you can always catch the exception in Parse :) Also, Parse is .NET 1.1, while TryParse is only .NET 2.0

              A Offline
              A Offline
              Amol Ravatale
              wrote on last edited by
              #6

              :)

              Every thing will come to you if you have faith.

              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