method should return DateTime but I want it to return null

D

Daves

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?
 
K

Kent Boogaart

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
 
V

Vivek

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
 

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

Similar Threads

Null and DateTime 8
SqlDateTime and DateTime 2
null DateTime variables 2
DateTime and null values 1
How to... 1
DateTime Null 2
Cast string to DateTime 3
How to represent "null" datetime ? 8

Top