Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Config helper ysing generics

Status
Not open for further replies.

Badgers

Programmer
Nov 20, 2001
187
US
Hi,

I have the following bit of code:

private static T GetConfigValue2<T>(string key)
{
int value = 0;
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings[key]))
{
int.TryParse(ConfigurationManager.AppSettings[key], out value);
}
return value;
}

I want to do an int.tryparse for int values, does anyone know how to test the type T for an int???

Also how to make the return of the function generic as the values would be string, int and double.

Thanks
 
Code:
private static T GetConfigValue2<T>(string key)
{
   var value = ConfigurationManager.AppSettings[key];
   return value == null 
                  ? default (T)
                  : (T)Convert.ChangeType(value, typeof(T));
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top