GetType() returns invalid value for DateTime?

A

Andrus

Code below returns False but must return True.
How to fix ?

Andrus.


using System;
using System.Windows.Forms;

class Starter
{
static void Main()
{
DateTime? x = new DateTime?(DateTime.Now);
Test(x);
}


static void Test(object ob)
{
MessageBox.Show((ob.GetType() == typeof(DateTime?)).ToString());
}
}
 
P

Peter Duniho

Code below returns False but must return True.
How to fix ?

Either get rid of "ToString()" on the right, or add it on the left.

Better still, don't compare the type names; use the "is" operator.

And even better, figure out a way to write the code so that you don't need
to compare the type of an object to anything. :)

Pete
 
A

Andrus

Peter,

Thank you.
Please note that posted code *does not compare* type names and adding
ToString() or using is operator does not change this issue.
Anyway I created changed testcase.

ob type is Datetime?.
Why code below outputs "Not DateTime?" and how to fix this ?

Andrus.


using System;
using System.Windows.Forms;


class Starter
{
static void Main()
{
DateTime? x = new DateTime?(DateTime.Now);
Test(x);
}


static void Test(object ob)
{
if (ob.GetType() != typeof(DateTime?))
MessageBox.Show("Not DateTime?");
}
}

----- Original Message -----
From: "Peter Duniho" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Monday, July 20, 2009 10:47 AM
Subject: Re: GetType() returns invalid value for DateTime?
 
G

Gregory A. Beamer

Code below returns False but must return True.
How to fix ?

Andrus.


using System;
using System.Windows.Forms;

class Starter
{
static void Main()
{
DateTime? x = new DateTime?(DateTime.Now);
Test(x);
}


static void Test(object ob)
{
MessageBox.Show((ob.GetType() ==
typeof(DateTime?)).ToString());
}
}

This will return you true:

static void Test(object ob)
{
MessageBox.Show((ob.GetType() == typeof(DateTime)).ToString());
}

As, once loaded, it is a DateTime.

The reason this works, and the DateTime? does not, is the GetType()
actually gets the underlying type.

For example:

//Returns ob.GetType() = System.DateTime
Console.WriteLine("ob.GetType() = {0}", ob.GetType());

But, if you try this:

DateTime? ob2 = null;
Console.WriteLine("ob2.GetType() = {0}", ob2.GetType());

it will blow chunks.

The proper way around this is testing that the object is actually
loaded:

if(ob2.HasValue)
{
}

Also note that this:

DateTime? x = new DateTime?(DateTime.Now);

is more complex than needed, you can do this:

DateTime? x = DateTime.Now;

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

Peter,

Thank you.
Please note that posted code *does not compare* type names and adding
ToString() or using is operator does not change this issue.
Anyway I created changed testcase.

My apologies. I misread the original code. I see that others who replied
with more care (and sleep! :) ) have explained the issue.

I stand by my previous suggestion to avoid type comparisons when
possible. :)

Pete
 
A

Andrus

Thank you.
The proper way around this is testing that the object is actually
loaded:

if(ob2.HasValue)


Why this is proper way? Is'nt it simpler to use

if(ob2 != null )

In this case HasValue property can be removed from Nullable<> struct at
all.

Andrus.
 
G

Gregory A. Beamer

Thank you.



Why this is proper way? Is'nt it simpler to use

if(ob2 != null )

In this case HasValue property can be removed from Nullable<> struct
at all.

I will probably flub up the explanation a bit, but here goes.


There are two basic types in .NET base types. Value types (all of your
integral values, DateTime, structures you create, etc.) which are stored
as structures and placed on your application stack memory. There are
also reference types (any class you created, strings, etc), which are
stored on the heap.

When Microsoft first created .NET, the base types that were not
reference types had no concept of null. But there was a call for
nullable types, so MS had two choices:

1) Break all existing code by refactoring the value types to have a
null, so you could test like so

if(MyDateTime == null)

2) Create a generic wrapper class to create a nullable type without
altering the current structures, which requires the following test

if(!MyDateTime.HasValue)

Wisely, in my opinion, MS choose the later. Perhaps with more time to
get it done they could have figured a way of doing this without breaking
current code, but it works.

yes the !=null test would be more consistent, but breaking all of the
1.x code was a non-option, if you ask me.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top