|
-
Nov 29th, 2010, 01:51 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Nullable datatype inhibits formatting?
I'm trying to format a date. This works:
Dim x As Date = Today
Dim y As String
y = x.ToString("MMMM d, yyyy")
But this doesn't:
Dim x As Date? = Today
Dim y As String
y = x.ToString("MMMM d, yyyy")
When x is nullable I get an error 'Overload resolution failed because no accessible ToString accepts this number of arguments'.
I suppose I could copy the nullable variable into a non-nullable variable, then format it, but that seems extremely clumsy for something that should be fairly simply. Isn't there a better way?
Thanks.
Arghh! Copying the Date? variable into a Date variable doesn't work-Option Strict is On & that sees the two variables as being of different types. Is it really necessary to to an explicit CDate conversion on a nullable date variable?
OK, found an example that works. Problem is I was calling ToString directly on the nullable Date? variable-what I need to do is call it on the Value of that:
y = x.Value.ToString("MMMM d, yyyy")
That works.
Last edited by calvin-c; Nov 29th, 2010 at 03:00 PM.
Reason: Solved
-
Nov 29th, 2010, 06:41 PM
#2
Re: [RESOLVED] Nullable datatype inhibits formatting?
Did it never occur to you to read the documentation?
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx
Fundamental Properties
The two fundamental members of the Nullable(Of T) structure are the HasValue and Value properties. If the HasValue property for a Nullable(Of T) object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.
It should have taken about 2 minutes to find the answer. Instead it apparently took you over an hour, because you didn't look in the obvious place first. You won't always find what you need in the documentation. If you don't then you've only wasted a couple of minutes. If you do then you have probably saved much more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|