Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Working with DateTime in .Net

1 view
Skip to first unread message

Johan Burger

unread,
Jul 14, 2005, 4:55:02 AM7/14/05
to
In Delphi for Windows the TDateTime is in essence a numeric field thus
statements such as "myDate := 0;" and "if myDate = 0 then..." are allowed.

I'm trying to achieve something similar with the DateTime object in .Net but
for some reason it's just not behaving the way I would expect it to.
If've tried things like "myDate := convert.ToDateTime(nil);" which worked,
but then if I say "if &Object(myDate) = nil then..." the if translates to a
False where I'm expecting it to be True. I also haven't found any methods
which I can use to set and test for nil. Converting a zero to a DateTime
also raises exceptions.

My question is this: How do I set a date to nothing (either nil or 0) and
secondly, how do I test for the nothing value?

Many thanks


Dirk Andries

unread,
Jul 14, 2005, 5:58:17 AM7/14/05
to
Johan Burger wrote:

1) System.DateTime is a value type. There is no way you can set it to
nothing or nil.
2) You can store it in a variable as System.Object (thus boxed
version). In this case you can assign nil to the variable and test
with Assigned.
3) You can use your own nullable types (or download a .NET Nullables
library somewhere).
Basically a nullable value type is a type which contains a value
(DateTime in your case) and a Boolean property IsNull wich you can test.
Nullables will be available in the .NET 2.0 framework.
4) You could use 'magic values':
System.DateTime.MinValue (default value for a System.DateTime)
System.DateTime.MaxValue

It is a certainly a good idea to get some background on the .NET type
system, especially on Value types, Reference types and System.String
(immutable!).


Kind regards,
Dirk Andries

Johan Burger

unread,
Jul 18, 2005, 5:18:31 AM7/18/05
to
Excellent, thanks!

"Dirk Andries" <nos...@nospam.be> wrote in message
news:42d6...@newsgroups.borland.com...

0 new messages