I have a method to parse strings
. It can be used to parse a float
using a specific culture
and then get a new string
with a specific out-culture
. The same applies to ints
, doubles
and decimals
.
The code that I've written is quite repetitive for each of the different parse methods which makes it hard to maintain (especially as I am just about to make the method a lot more complex).
Is it possible to make this code less repetitive?
if (mapping.ParseDecimal)
{
decimal i;
if (decimal.TryParse(value, numberStyle, inCulture, out i))
{
return i.ToString(outCulture);
}
else
{
if (logger.IsDebugEnabled)
logger.DebugFormat("Could not parse value \"{0\" to a decimal using the culture \"{1}\".", value, inCulture);
}
}
else if (mapping.ParseDouble)
{
double i;
if (double.TryParse(value, numberStyle, inCulture, out i))
{
return i.ToString(outCulture);
}
else
{
if (logger.IsDebugEnabled)
logger.DebugFormat("Could not parse value \"{0\" to a double using the culture \"{1}\".", value, inCulture);
}
}
else if (mapping.ParseFloat)
{
float i;
if (float.TryParse(value, numberStyle, inCulture, out i))
{
return i.ToString(outCulture);
}
else
{
if (logger.IsDebugEnabled)
logger.DebugFormat("Could not parse value \"{0\" to a float using the culture \"{1}\".", value, inCulture);
}
}
else if (mapping.ParseInt)
{
int i;
if (int.TryParse(value, numberStyle, inCulture, out i))
{
return i.ToString(outCulture);
}
else
{
if (logger.IsDebugEnabled)
logger.DebugFormat("Could not parse value \"{0\" to a int using the culture \"{1}\".", value, inCulture);
}
}