locked
check if variable is number in C# RRS feed

  • Question

  • User457574343 posted

    What's the best and easy way to check if a variable is a number in C#?

    Thanks a lot :)

    Wednesday, January 21, 2009 2:35 PM

Answers

  • User533502567 posted

    I think parsing is easy solution... 

     int Num;
     bool isNum = int.TryParse(c.ToString (), out Num); //c is your variable
     if (isNum)
      Response.Write(c); //integer
     else
      Response.Write("Not an interger"); 
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 21, 2009 2:43 PM
  • User188494879 posted

     You can use Int.TryParse

    as:

    http://msdn.microsoft.com/en-us/library/f02979c7.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 21, 2009 2:44 PM
  • User188494879 posted

      

           if (!String.IsNullOrEmpty(Request.QueryString["YourValue"]))
            {
                int intValue;
                bool myValue = int.TryParse(Request.QueryString["YourValue"],out intValue);
                if(myValue)
                {
                    //your logic
                }
                else
                {
                    //your logic
                }
            }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 21, 2009 3:08 PM

All replies

  • User533502567 posted

    I think parsing is easy solution... 

     int Num;
     bool isNum = int.TryParse(c.ToString (), out Num); //c is your variable
     if (isNum)
      Response.Write(c); //integer
     else
      Response.Write("Not an interger"); 
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 21, 2009 2:43 PM
  • User188494879 posted

     You can use Int.TryParse

    as:

    http://msdn.microsoft.com/en-us/library/f02979c7.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 21, 2009 2:44 PM
  • User1131927913 posted

    Is this a User input, or are you reading from a Table?

    -smc

    Wednesday, January 21, 2009 2:57 PM
  • User457574343 posted
    Hi, thank you.

    I want to check the value in querystring.
    Wednesday, January 21, 2009 3:00 PM
  • User188494879 posted

      

           if (!String.IsNullOrEmpty(Request.QueryString["YourValue"]))
            {
                int intValue;
                bool myValue = int.TryParse(Request.QueryString["YourValue"],out intValue);
                if(myValue)
                {
                    //your logic
                }
                else
                {
                    //your logic
                }
            }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 21, 2009 3:08 PM
  • User1622111507 posted

    Thanks for the code, Karan...

    Saturday, July 24, 2010 4:11 PM
  • User-1540909049 posted

    It's a bit more elegant to do the tryParse inside the if statement

    Thursday, July 5, 2012 11:51 AM