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

Tagged as

Enhance String type to get Converted In Given Type

, 28 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Enhance String type to get Converted In Given Type

Problem 


Recently I was working on the project where I need to read the excel file data and have to convert data of it in strong entity which is consist of number of property. For this I know the sequence of the data in excel file and name & type of property of the entity which is going to store the value. But the real problem is I need to convert the string value in property type.

Solution

One of the solution to this is create function and pass the type & string value which
return data in type which is passed. Below is implementation function

public static T ConvertData<T>(this string s)
{
     if(!sting.IsNullOrEmpty(s))
     {
        if (typeof(T) == typeof(System.Decimal))
        {
           return (T)(object)Convert.ToDecimal(s);
        }
     }
     //same code get replicated for int, float etc. 
     return default(T);
}

In above code I created on generic extension method for string type. Function convert string type to the type I want.

Cons


  • In above implementation I written code for Decimal type but same code I have to replicate for other types also i.e for int, float etc.
  • As you see in code after converting data in given type I again need to reconvert into object and than into type T again. This also add overhead of type casting.
  • Code is become long and unclear.
 
So to make code clean , clear and easy to understand. I fond one method in C#.net which is Convert.ChangeType which helps me to create the method i want easily.
Below is my Simpler and easy solution
public static T ConvertData<T>(this string s)
{
     if(!sting.IsNullOrEmpty(s))
     {
         return (T)Convert.ChangeType(s, typeof(T),null);
     }
     return default(T);
}
So in above solution I just need to write one line of the code which do the task for me.
Actual syntax of method is
public static Object ChangeType(
 Object value,
 Type conversionType,
 IFormatProvider provider
)
As you can see in syntax third parameter is provider which is null in my case but you can pass the formater by creating as you need.
Now following code is just show how to use this method in code i.e ConvertData method
string s = "123";
int a = s.ConvertData<int>();
So this method can be used any project and also fit in number of requirement.

License

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

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
SuggestionTypo PinmemberAmbre Sachin29-Oct-12 3:33 

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
Web04 | 2.8.140827.1 | Last Updated 29 Oct 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid