Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immediate Window - Cast as datetime? throws exception but (datetime) doesn't

Taken directly from the immediate window:

reader["DateDue"] as DateTime? yields:

'reader["DateDue"] as DateTime?' threw an exception of type 'System.NullReferenceException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147467261
HelpLink: null
InnerException: null
Message: "Object reference not set to an instance of an object."
Source: null
StackTrace: null
TargetSite: null

(DateTime?)reader["DateDue"] yields:

{1/26/2015 12:00:00 AM}
Date: {1/26/2015 12:00:00 AM}
Day: 26
DayOfWeek: Monday
DayOfYear: 26
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 0
Month: 1
Second: 0
Ticks: 635578272000000000
TimeOfDay: {System.TimeSpan}
Year: 2015

and for reference, reader["DateDue"] yields:

{1/26/2015 12:00:00 AM}
Date: {1/26/2015 12:00:00 AM}
Day: 26
DayOfWeek: Monday
DayOfYear: 26
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 0
Month: 1
Second: 0
Ticks: 635578272000000000
TimeOfDay: {00:00:00}
Year: 2015

Is this a bug? If directly casting to DateTime? works then casting with as DateTime? should work too.

I did find a work around for this by using reader.GetDateTime(reader.GetOrdinal("DateDue")) as DateTime?. Nevermind, that doesn't handle nulls well. Anyway there's myriad ways to paper around this oddity.

Repo demonstrating the issue can be found here: https://github.com/jjoedouglas/exceptionAsDatetime

like image 226
MushinNoShin Avatar asked Jul 27 '15 04:07

MushinNoShin


People also ask

What is the purpose of immediate window and watch window in Visual Basic?

Use the Immediate window to debug and evaluate expressions, execute statements, and print variable values. The Immediate window evaluates expressions by building and using the currently selected project.

Where is immediate window in Visual Studio?

Press Ctrl + Q to focus on Quick Launch text box. Type Immediate then choose the item that appears: Debug -> Windows -> Immediate ( Ctrl+Alt+I )

How do I display a variable value in immediate window?

Working With Variables To view the variable's value, simply type its name into the immediate window and press Enter. The value, "Hello world" will be displayed beneath the typed line, as in the image above. To access properties of a variable or value, use the member access operator (.) as you would within source code.


1 Answers

This appears to be a bug in the VS2015 immediate window.

like image 98
MushinNoShin Avatar answered Oct 03 '22 08:10

MushinNoShin