By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

method should return DateTime but I want it to return null

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.