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. using IsNumeric Function in c#

using IsNumeric Function in c#

Scheduled Pinned Locked Moved C#
csharphelptutorial
16 Posts 8 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.
  • S Offline
    S Offline
    sumit7034
    wrote on last edited by
    #1

    how to use IsNumeric Function in c# it is working fine in vb.net. i think some namespace should be included. please help me. thanks

    M L M N L 5 Replies Last reply
    0
    • S sumit7034

      how to use IsNumeric Function in c# it is working fine in vb.net. i think some namespace should be included. please help me. thanks

      M Offline
      M Offline
      Mogaambo
      wrote on last edited by
      #2

      There is no such function in c# but u can create one try { int num=Integer.parse("10"); return num; } catch(Exception ex) { return "Invalid number"; }

      1 Reply Last reply
      0
      • S sumit7034

        how to use IsNumeric Function in c# it is working fine in vb.net. i think some namespace should be included. please help me. thanks

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

        add a reference : "Microsoft.VisualBasic" then use: Microsoft.VisualBasic.Information.IsNumeric(...);

        S 1 Reply Last reply
        0
        • S sumit7034

          how to use IsNumeric Function in c# it is working fine in vb.net. i think some namespace should be included. please help me. thanks

          M Offline
          M Offline
          mark_w_
          wrote on last edited by
          #4

          I would use Integer.tryparse rather than Integer.parse http://msdn.microsoft.com/en-us/library/f02979c7.aspx[^]

          1 Reply Last reply
          0
          • L Lost User

            add a reference : "Microsoft.VisualBasic" then use: Microsoft.VisualBasic.Information.IsNumeric(...);

            S Offline
            S Offline
            sumit7034
            wrote on last edited by
            #5

            thanks

            L 1 Reply Last reply
            0
            • S sumit7034

              how to use IsNumeric Function in c# it is working fine in vb.net. i think some namespace should be included. please help me. thanks

              N Offline
              N Offline
              Nirandas
              wrote on last edited by
              #6

              Use int.TryParse() method.

              string str = "123";
              nt num;
              if( int.TryParse( str, out num))
              {//successfully parsed the string. Now the num variable contains the numeric value if you need it.
              //Ok the number is numeric do what ever you want.
              }

              Nirandas, a developer from India. http://www.nirandas.com

              1 Reply Last reply
              0
              • S sumit7034

                how to use IsNumeric Function in c# it is working fine in vb.net. i think some namespace should be included. please help me. thanks

                L Offline
                L Offline
                Le centriste
                wrote on last edited by
                #7

                I think Regex would be the way to go: return Regex.IsMatch(yourText, "^\d+$"); This would work for simple numbers.

                1 Reply Last reply
                0
                • S sumit7034

                  thanks

                  L Offline
                  L Offline
                  Le centriste
                  wrote on last edited by
                  #8

                  You should try to avoid using functions from the VisualBasic namespace.

                  L 1 Reply Last reply
                  0
                  • L Le centriste

                    You should try to avoid using functions from the VisualBasic namespace.

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Le Centriste wrote:

                    You should try to avoid using functions from the VisualBasic namespace.

                    So why do you say that? Is that namespace inferior?

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    L 1 Reply Last reply
                    0
                    • L leppie

                      Le Centriste wrote:

                      You should try to avoid using functions from the VisualBasic namespace.

                      So why do you say that? Is that namespace inferior?

                      xacc.ide - now with TabsToSpaces support
                      IronScheme - 1.0 alpha 4a out now (29 May 2008)

                      L Offline
                      L Offline
                      Le centriste
                      wrote on last edited by
                      #10

                      This package is used to support the VisualBasic runtime.

                      L 1 Reply Last reply
                      0
                      • L Le centriste

                        This package is used to support the VisualBasic runtime.

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #11

                        Yes I know that, but what is wrong with it? Why should you not use it?

                        xacc.ide - now with TabsToSpaces support
                        IronScheme - 1.0 alpha 4a out now (29 May 2008)

                        L 1 Reply Last reply
                        0
                        • L leppie

                          Yes I know that, but what is wrong with it? Why should you not use it?

                          xacc.ide - now with TabsToSpaces support
                          IronScheme - 1.0 alpha 4a out now (29 May 2008)

                          L Offline
                          L Offline
                          Le centriste
                          wrote on last edited by
                          #12

                          Because there are better alternatives in the .NET library. Furthermore, they are legacy. We had a guy here who was using the CInt function to simply convert a string into an integer (he was using VB.NET and was a former VB programmer). His algorithm seemed to be working well but the numbers were not right. I suggested that he uses Int32.Parse instead, just for the heck of it. Coincidentally, Int32.Parse threw an exception instead of converting the number. It turned out the string was not a number, but the scientific representation of a number. CInt was making the best of it by taking the interger part of the number, which was wrong in this case. The reason why Microsoft created VB.NET and added support for the VB runtime is because they wanted to lure VB programmers and make them embrace the .NET platform.

                          D 1 Reply Last reply
                          0
                          • L Le centriste

                            Because there are better alternatives in the .NET library. Furthermore, they are legacy. We had a guy here who was using the CInt function to simply convert a string into an integer (he was using VB.NET and was a former VB programmer). His algorithm seemed to be working well but the numbers were not right. I suggested that he uses Int32.Parse instead, just for the heck of it. Coincidentally, Int32.Parse threw an exception instead of converting the number. It turned out the string was not a number, but the scientific representation of a number. CInt was making the best of it by taking the interger part of the number, which was wrong in this case. The reason why Microsoft created VB.NET and added support for the VB runtime is because they wanted to lure VB programmers and make them embrace the .NET platform.

                            D Offline
                            D Offline
                            Dave Doknjas
                            wrote on last edited by
                            #13

                            It wouldn't have been CInt since CInt isn't a function - it's a 'macro' built into the VB language. You couldn't use it from C# even if you wanted it. Same goes for CType, CDbl, CStr, DirectCast, etc.

                            David Anton http://www.tangiblesoftwaresolutions.com C++ to C# Converter C++ to VB Converter C++ to Java Converter VB & C# to Java Converter Java to VB & C# Converter Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: convert VB, C#, or Java to C++/CLI

                            L 2 Replies Last reply
                            0
                            • D Dave Doknjas

                              It wouldn't have been CInt since CInt isn't a function - it's a 'macro' built into the VB language. You couldn't use it from C# even if you wanted it. Same goes for CType, CDbl, CStr, DirectCast, etc.

                              David Anton http://www.tangiblesoftwaresolutions.com C++ to C# Converter C++ to VB Converter C++ to Java Converter VB & C# to Java Converter Java to VB & C# Converter Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: convert VB, C#, or Java to C++/CLI

                              L Offline
                              L Offline
                              Le centriste
                              wrote on last edited by
                              #14

                              You are right, the guy was using VB.NET, I forgot to mention. But what I said is still valid, even with VB.NET.

                              1 Reply Last reply
                              0
                              • D Dave Doknjas

                                It wouldn't have been CInt since CInt isn't a function - it's a 'macro' built into the VB language. You couldn't use it from C# even if you wanted it. Same goes for CType, CDbl, CStr, DirectCast, etc.

                                David Anton http://www.tangiblesoftwaresolutions.com C++ to C# Converter C++ to VB Converter C++ to Java Converter VB & C# to Java Converter Java to VB & C# Converter Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: convert VB, C#, or Java to C++/CLI

                                L Offline
                                L Offline
                                Le centriste
                                wrote on last edited by
                                #15

                                In fact, I re-read my post and I actually mentioned the guy was using VB.NET.

                                D 1 Reply Last reply
                                0
                                • L Le centriste

                                  In fact, I re-read my post and I actually mentioned the guy was using VB.NET.

                                  D Offline
                                  D Offline
                                  Dave Doknjas
                                  wrote on last edited by
                                  #16

                                  Sorry - I should have read the post more carefully.

                                  David Anton http://www.tangiblesoftwaresolutions.com C++ to C# Converter C++ to VB Converter C++ to Java Converter VB & C# to Java Converter Java to VB & C# Converter Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: convert VB, C#, or Java to C++/CLI

                                  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