Convert.ToDateTime(bool value) [modified]
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
For consistency would be my guess. The Convert.ToXXX methods usually support all the elementary types like Byte, Int16, Boolean etc. So, for those types where a conversion doesn't make any sense, InvalidCastException is thrown - though they may have been more clear if they added a new NoMatchingConversionException or something similar.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
I don't use Convert; I use int.Parse(), DateTime.Parse(), etc.
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
It's likely for signature consistency with the other routines in the Convert class. You're experiencing proper behavior. From Reflector: Convert.ToDateTime:
public static DateTime ToDateTime(bool value) { return ((IConvertible) value).ToDateTime(null); }
Boolean.ToDateTime:DateTime IConvertible.ToDateTime(IFormatProvider provider) { throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), new object[] { "Boolean", "DateTime" })); }
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
Would it not have been better to not include that method in the framework so we can get a compile time error as oppose to compiling such illogical behavior and having to deal with the exception at runtime. Then again why would someone want to convert a Boolean to datetime. Batman
-
Would it not have been better to not include that method in the framework so we can get a compile time error as oppose to compiling such illogical behavior and having to deal with the exception at runtime. Then again why would someone want to convert a Boolean to datetime. Batman
I understand that trying to convert from a boolean to DateTime is ilogical, but then, if you had to, what would the return value for this be ? DateTime dt = Convert.ToDateTime(true); DateTime dt2 = Convert.ToDateTime(false); What assumptions would you have to make to actually implement this method
The things we leave behind are nothing but... well i guess i'm not that wise after all....
-
I understand that trying to convert from a boolean to DateTime is ilogical, but then, if you had to, what would the return value for this be ? DateTime dt = Convert.ToDateTime(true); DateTime dt2 = Convert.ToDateTime(false); What assumptions would you have to make to actually implement this method
The things we leave behind are nothing but... well i guess i'm not that wise after all....
Something is only equal to itself and nothing else. So your reference must be a specific datetime as a basis of conversion to Boolean. You seem to be asking for a solution without a point of reference. In that case what is x if x * x = y. ... not enough info to solve.
-
Would it not have been better to not include that method in the framework so we can get a compile time error as oppose to compiling such illogical behavior and having to deal with the exception at runtime. Then again why would someone want to convert a Boolean to datetime. Batman
SpaghettiCoding wrote:
Would it not have been better to not include that method in the framework so we can get a compile time error as oppose to compiling such illogical behavior and having to deal with the exception at runtime.
I suspect that might have caused problems for VB.NET. But then, i use that rational for everything insane in the BCL. It's what keeps me lovin' .NET...
----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
Isn't this a programming question? I love the double standards here. I ask people to look at a post I made in a programming forum, and all I get is 1'd and insulted . This guy asks a REAL programming question, and everyone's ignoring the fact that he plainly broke the rules. Amazing.... well, not really.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
Sometimes nonsense is just nonsense. But, in any case, as John pointed out, this does not belong in the Lounge...
-
Isn't this a programming question? I love the double standards here. I ask people to look at a post I made in a programming forum, and all I get is 1'd and insulted . This guy asks a REAL programming question, and everyone's ignoring the fact that he plainly broke the rules. Amazing.... well, not really.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
This honestly belongs more over in the brand new What The...? forum[^]. :)
-
Sometimes nonsense is just nonsense. But, in any case, as John pointed out, this does not belong in the Lounge...
Perhaps in "What the..." then. Convert should be deprecated.
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
Surely the question is, why would anyone want to do this?
Deja View - the feeling that you've seen this post before.
-
Isn't this a programming question? I love the double standards here. I ask people to look at a post I made in a programming forum, and all I get is 1'd and insulted . This guy asks a REAL programming question, and everyone's ignoring the fact that he plainly broke the rules. Amazing.... well, not really.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001You see is not actually a question, It was something that I found funny, and wanted to share with everyone, also I was putting the theme on the table of what implementation and assumptions you would have to make in order to convert from boolean to DateTime and from DateTime to boolean, just wanted to make you think for a second.
The things we leave behind are nothing but... well i guess i'm not that wise after all....
-
Have you guys noticed that when you do Convert.ToDateTime the first overload of the function receives a boolean parameter, I was wondering what's the outcome of this, I tried it bool myVar = (Convert.ToDateTime(true))? true : false; DateTime dtVar = Convert.ToDateTime(true); bool boolVar = Convert.ToBoolean(DateTime.Now); and received Invalid Cast Exception, and then I had another question why isn't the compiler catching this invalid cast, well I haven't found a logical way to translate a true to a date time, and I don't expect to, but Why is this overload in the framework to begin with, and why is the error at runtime. Anyway any Ideas on what Date Time is True ??? And what Date Time is False ?? -- modified at 12:31 Monday 5th March, 2007
The things we leave behind are nothing but... well i guess i'm not that wise after all....
jcdevnet wrote:
Anyway any Ideas on what Date Time is True ??? And what Date Time is False ??
If you suffer from explosive diarrhea, maybe it's not time for a date :rolleyes:
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us!