Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#
Tip/Trick

Basic string to integer conversion extensions

Rate me:
Please Sign up or sign in to vote.
4.17/5 (3 votes)
21 Jun 2011CPOL 23.3K   5   2
String handling Extension Methods

I had some useful extension for strings. When I read this article Handy Extension Methods, I thought I'd share them.


In one of my projects, I had to validate a lot of TextBox values (e.g., value of TextBox txtItemSupplied must be an integer with value greater than 0).


I had written some basic extension methods for string to integer conversions. The methods were used for validating input data.


Here is the code:


C#
//validation strItems must be greater then 0.
static void ConversionTest()
{
    string strItems = null;//test input
    strItems = "0";//test input
    //previous check for integer greater then 0.
    int intItem;
    if (int.TryParse(strItems, out intItem))
    {
        if (intItem < 1)
            Console.WriteLine("Please enter valid value.");
    }
    //New check for integer greater then 0.
    if (strItems.ToInt(0) < 1)
    {
        Console.WriteLine("Please enter valid value.");
    }
}

Now this is the Extension class:


C#
public static class MyStringExtensions
{
    public static bool IsInt(this string text)
    {
        int num;
        return int.TryParse(text, out num);
    }
    //get null if conversion failed
    public static int? ToInt(this string text)
    {
        int num;
        return int.TryParse(text, out num) ? num : (int?)null;
    }
    //get default value if conversion failed
    public static int ToInt(this string text,int defaultVal)
    {
        int num;
        return int.TryParse(text, out num) ? num : defaultVal;
    }

    //formating a string
    public static string StringFormat(this string text, params object[] formatArgs)
    {
        return string.Format(text, formatArgs);
    }


}


Another useful Extension method.
Returns whether DataSet contains some DataRows or not.

C#
public static class DataSetExtensions
    {
    //Return whether DataSet contains DataRow(s) or not
        public static bool IsNullOrEmpty(this DataSet ds)
        {
            return (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0);
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 4 Useful wrappers though not actually ... Pin
Ed Nutting14-Jun-11 9:13
Ed Nutting14-Jun-11 9:13 
QuestionPlease follow framework naming conventions Pin
Member 74933922-Jun-11 21:17
Member 74933922-Jun-11 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.