471,315 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

method should return DateTime but I want it to return null

a get { ... } for public property SelectedValue returns DateTime type to be
used as a parameter in a Sql update query but I'd like it to return "empty"
if no date has been selected...
I cannot use
return null;

because "Cannot convert null to 'System.DateTime' because it is a value
type"

How can this be done?
Jan 31 '06 #1
3 1788
If you're using C# 2 then you can use the Nullable generic type:

public DateTime? SelectedValue
{
get / set;
}

Note the question mark which is short-hand for Nullable<DateTime>. See the
Nullable API doc for more details. If you're using C# 1.0 then you'll need
to either store in a reference type (object) or pick a DateTime to equate to
null (eg. DateTime.MinValue). Alternatively, use an extra bool field to
indicate null.

HTH,
Kent

"Daves" <db****@simnet.is> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
a get { ... } for public property SelectedValue returns DateTime type to be
used as a parameter in a Sql update query but I'd like it to return "empty"
if no date has been selected...
I cannot use
return null;

because "Cannot convert null to 'System.DateTime' because it is a value
type"

How can this be done?

Jan 31 '06 #2
You can also send the datetime var with out parameter and get the boolean
result like this :

bool Fx(out varDateTime)
{
//return bool;
}
if bool is true, varDateTime is not null

Vivek

Le relais des Artistes (Esvicom - Lieu de Vie, ATBM - Théâtre du Bout des
Mains, DW Home Studio) vous souhaite une très bonne année 2006
http://www.relaisdesartistes.com
"Kent Boogaart" <ke****@internode.on.net> a écrit dans le message de news:
%2****************@TK2MSFTNGP11.phx.gbl...
If you're using C# 2 then you can use the Nullable generic type:

public DateTime? SelectedValue
{
get / set;
}

Note the question mark which is short-hand for Nullable<DateTime>. See the
Nullable API doc for more details. If you're using C# 1.0 then you'll need
to either store in a reference type (object) or pick a DateTime to equate
to null (eg. DateTime.MinValue). Alternatively, use an extra bool field
to indicate null.

HTH,
Kent

"Daves" <db****@simnet.is> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
a get { ... } for public property SelectedValue returns DateTime type to
be used as a parameter in a Sql update query but I'd like it to return
"empty" if no date has been selected...
I cannot use
return null;

because "Cannot convert null to 'System.DateTime' because it is a value
type"

How can this be done?


Jan 31 '06 #3
thx guys, I think I have c# v2, both of your solutions are great.

"Vivek" <vi**************@msn.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
You can also send the datetime var with out parameter and get the boolean
result like this :

bool Fx(out varDateTime)
{
//return bool;
}
if bool is true, varDateTime is not null

Vivek

Le relais des Artistes (Esvicom - Lieu de Vie, ATBM - Théâtre du Bout des
Mains, DW Home Studio) vous souhaite une très bonne année 2006
http://www.relaisdesartistes.com
"Kent Boogaart" <ke****@internode.on.net> a écrit dans le message de news:
%2****************@TK2MSFTNGP11.phx.gbl...
If you're using C# 2 then you can use the Nullable generic type:

public DateTime? SelectedValue
{
get / set;
}

Note the question mark which is short-hand for Nullable<DateTime>. See
the Nullable API doc for more details. If you're using C# 1.0 then you'll
need to either store in a reference type (object) or pick a DateTime to
equate to null (eg. DateTime.MinValue). Alternatively, use an extra bool
field to indicate null.

HTH,
Kent

"Daves" <db****@simnet.is> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
a get { ... } for public property SelectedValue returns DateTime type to
be used as a parameter in a Sql update query but I'd like it to return
"empty" if no date has been selected...
I cannot use
return null;

because "Cannot convert null to 'System.DateTime' because it is a value
type"

How can this be done?



Jan 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Abhishek Srivastava | last post: by
6 posts views Thread by Millo | last post: by
reply views Thread by Vagabond Software | last post: by
reply views Thread by Simple Simon | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.