snippetcsharpModerate
Same code to parse int, float, decimal?
Viewed 0 times
samefloatdecimalparsecodeint
Problem
I have a method to parse
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?
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);
}
}Solution
If repetition is your primary concern, you could try doing something like this:
This way, you only have to pass around the appropriate
public delegate string ParserMethod(string value, NumberStyles numberStyle, CultureInfo inCulture, CultureInfo outCulture);
public static class NumericParser
{
public static readonly ParserMethod ParseInt = Create(int.TryParse);
public static readonly ParserMethod ParseFloat = Create(float.TryParse);
public static readonly ParserMethod ParseDouble = Create(double.TryParse);
public static readonly ParserMethod ParseDecimal = Create(decimal.TryParse);
public static Logger Logger { get; set; }
delegate bool TryParseMethod(string s, NumberStyles style, IFormatProvider provider, out T result);
static ParserMethod Create(TryParseMethod tryParse) where T : IFormattable
{
return (value, numberStyle, inCulture, outCulture) =>
{
T result;
if (tryParse(value, numberStyle, inCulture, out result))
{
return result.ToString(null, outCulture);
}
else
{
if (Logger != null && Logger.IsDebugEnabled)
Logger.DebugFormat("Could not parse value \"{0}\" to a {1} using the culture \"{2}\".", value, typeof(T).Name, inCulture);
return "";
}
};
}
}This way, you only have to pass around the appropriate
ParserMethod you want to use. In your case, you could map your different mapping values to the appropriate ParserMethod. And call it when needed.Code Snippets
public delegate string ParserMethod(string value, NumberStyles numberStyle, CultureInfo inCulture, CultureInfo outCulture);
public static class NumericParser
{
public static readonly ParserMethod ParseInt = Create<int>(int.TryParse);
public static readonly ParserMethod ParseFloat = Create<float>(float.TryParse);
public static readonly ParserMethod ParseDouble = Create<double>(double.TryParse);
public static readonly ParserMethod ParseDecimal = Create<decimal>(decimal.TryParse);
public static Logger Logger { get; set; }
delegate bool TryParseMethod<T>(string s, NumberStyles style, IFormatProvider provider, out T result);
static ParserMethod Create<T>(TryParseMethod<T> tryParse) where T : IFormattable
{
return (value, numberStyle, inCulture, outCulture) =>
{
T result;
if (tryParse(value, numberStyle, inCulture, out result))
{
return result.ToString(null, outCulture);
}
else
{
if (Logger != null && Logger.IsDebugEnabled)
Logger.DebugFormat("Could not parse value \"{0}\" to a {1} using the culture \"{2}\".", value, typeof(T).Name, inCulture);
return "";
}
};
}
}Context
StackExchange Code Review Q#3686, answer score: 15
Revisions (0)
No revisions yet.