Thursday, June 25, 2009

How to convert string to int

        //Int32.TryParse Method (String, Int32%)
        // This method Converts the string representation of a number to its 32-bit signed integer equivalent,it return whether conversion succeeded or not
        int num;
        string[] value = { "123", "test", "-123" };
        for (int i = 0; i < value.Length - 1; i++)
        {
            bool result = Int32.TryParse(value[i].ToString(), out num);
            if (result)
            {
                Response.Write("String " + value[i].ToString() + " Converted Successfully");
            }
            else
            {
                if (value[i] == null) value[i] = "";
                Response.Write("Attempted For " + value[i] + " failed.");
            }
        }



No comments:

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.