Click here to Skip to main content
Click here to Skip to main content

Tagged as

Basic string to integer conversion extensions

, 21 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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:

//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:

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.
 
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)

Share

About the Author

leopard447

Pakistan Pakistan
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 21 Jun 2011
Article Copyright 2011 by leopard447
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid