how to check typ in string
-
hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like
if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double }
when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan. -
hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like
if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double }
when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan. -
hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like
if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double }
when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan. -
what does the statement look like? because DateTime dt; datetime.tryparse(input, dt); returns an error, it does not like the dt statement
-
hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like
if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double }
when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan.hi, you want to check data type of parameter. solution: string sf = "hello"; double d = 34.3; if (sf is string) MessageBox.Show("String"); else MessageBox.Show("SF not string"); if (sf is double) MessageBox.Show("double"); else MessageBox.Show("SF not double"); if (d is string) MessageBox.Show("string"); else MessageBox.Show("dnot string"); if (d is double) MessageBox.Show("double"); else MessageBox.Show("d not double"); :rose:
-
what does the statement look like? because DateTime dt; datetime.tryparse(input, dt); returns an error, it does not like the dt statement
stephan_007 wrote:
returns an error, it does not like the dt statement
Funnily enough, compilers don't have emotions. It neither "likes" nor "dislikes" an argument! Do you mean it throws an exception? do you think this exception might be usful to us in trying to answer your question? Have you tried the documentation for DateTime.TryParse? If you did you'd see the second parameter is defined as an out parameter.
DateTime dt; datetime.tryparse(input, out dt);
-
stephan_007 wrote:
returns an error, it does not like the dt statement
Funnily enough, compilers don't have emotions. It neither "likes" nor "dislikes" an argument! Do you mean it throws an exception? do you think this exception might be usful to us in trying to answer your question? Have you tried the documentation for DateTime.TryParse? If you did you'd see the second parameter is defined as an out parameter.
DateTime dt; datetime.tryparse(input, out dt);
ups stupid me :) yes you are right, now it works ;)
-
hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like
if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double }
when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan.Why is the function using a string as parameter? That means that you have to convert all values to strings, then parse them back to the values again. Make the parameter an object instead, then you can easily check the actual type of the value:
public int SomeFunction(object value) {
if (value is DateTime) {
DateTime d = (DateTime)value;
...
} else if (value is double) {
double d = (double)value;
...
} else {
throw new ArgumentException("Type "+value.GetType().Name+" is not accepted as parameter.");
}
}Using an object parameter to accept value types means that the values will be boxed. Boxing is something that you generally want to avoid if possible, but in this case there is no common base type between DateTime and Double that you could use instead. Also, converting to a string and back is far worse than boxing.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Friday, May 2, 2008 9:04 AM
-
Why is the function using a string as parameter? That means that you have to convert all values to strings, then parse them back to the values again. Make the parameter an object instead, then you can easily check the actual type of the value:
public int SomeFunction(object value) {
if (value is DateTime) {
DateTime d = (DateTime)value;
...
} else if (value is double) {
double d = (double)value;
...
} else {
throw new ArgumentException("Type "+value.GetType().Name+" is not accepted as parameter.");
}
}Using an object parameter to accept value types means that the values will be boxed. Boxing is something that you generally want to avoid if possible, but in this case there is no common base type between DateTime and Double that you could use instead. Also, converting to a string and back is far worse than boxing.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Friday, May 2, 2008 9:04 AM
I think I asked him that same thing a few days ago.