1

Is it possible to derive the intended object type from a .NET format string in C#? For example:

//Input parameters
var formatString = "dd/MM/yyyy";  //or "£0.00" for example
var stringValue = "2014-04-01"; //or "37.34" for example

//Logic
object value = null;

if (IsADate(formatString)) {
    objectValue = DateTime.Parse(stringValue);

} else if (IsANumber(formatString)) {
    objectValue = double.Parse(stringValue);

} else //etc

//Result
var resultString = String.Format(value,formatString);

I could obviously write out the IsADate() and IsANumber() functions, but wondered whether there is anything in the Framework that covers this already?

1
  • Do you just want to parse the stringValue or do you really need a function that maps a formatString to a Type? Commented Jul 17, 2014 at 9:27

2 Answers 2

3

DateTime.TryParseExact() will make you your IsDate() function and Int32.TryParse() will make you your IsNumber() function.

Finally you should have something like that :

DateTime dateTime ; 
int anInt;

if (DateTime.TryParseExact(formatString, stringValue , out dateTime)) {
    objectValue = DateTime.Parse(stringValue);

} else if (Int32.TryParse(stringValue , out anInt)) {
    objectValue = double.Parse(stringValue);

} else //etc
Sign up to request clarification or add additional context in comments.

5 Comments

TryParse already outputs the DateTime if it succeeds, no need to Parse it a second time. objectValue = dateTime should be sufficient.
What I don't understand about your answer: You are trying to convert dd/MM/yyyy into a DateTime? That won't work! You can not parse the format string. I guess what you want is TryParseExact(formatString, inputValue, out dateTime), which uses the format string to parse the given input into an ouput. If that works, chances are the format string was for a DateTime/int etc. And why do you double.Parse if you check whether it is an Int32?
You should use DateTime.TryParseExact with the given formatString and stringValue and for ints your answer won't work.
@Dirk : Thanks for your corretion.
@Ksv3n How will Int32.TryParse("d", out anInt); ever return true? d is a valid numeric format string, but fails to be parsed as a numeric value.
0

You could force .NET to try and parse your input given the format string where possible. If that succeeds, chances are good you got the right type:

var formatString = "dd/MM/yyyy";  //or "£0.00" for example
var stringValue = "2014-04-01"; //or "37.34" for example

//Logic
object value = null;
DateTime dtDummy;
int intDummy;

if (DateTime.TryParseExact(formatString, stringValue, out dtDummy)) {
    value = dtDummy;

} 
else
...

For some types (numeric types, for example) there's no such method.

2 Comments

Numbers (int, double, etc.) don't have a static method accepting a format string for parsing.
True. Then it's not possible to do that for numeric types, really.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.