Optional date parameter initialization

G

Guest

I am migrating my first vb6 app to vb.net 2003.

In the vb6 app a number of subs had optional date parameters:
Sub Fred ( ... ,optional FromDate as Date = 0...)

I need something like zero in there to see if the parameter has been used or
not.

In vb.net the imported code fails with:
Value of type 'Integer' cannot be converted to 'Date'

I have to supply a default value for the date - is there anything I can use
that obviously indicates that the parameter has not been supplied by the
caller?
 
M

Mattias Sjögren

I have to supply a default value for the date - is there anything I can use
that obviously indicates that the parameter has not been supplied by the
caller?

No, you either have to pick one date value to magically represent the
missing state, or use method overloading.


Mattias
 
H

Herfried K. Wagner [MVP]

John Austin said:
In the vb6 app a number of subs had optional date parameters:
Sub Fred ( ... ,optional FromDate as Date = 0...)

I need something like zero in there to see if the parameter has been used
or
not.

In vb.net the imported code fails with:
Value of type 'Integer' cannot be converted to 'Date'

I have to supply a default value for the date - is there anything I can
use
that obviously indicates that the parameter has not been supplied by the
caller?

I have recently written an article in German about supporting nullable date
and time values. Maybe you'll find some of the code snippets useful:

<URL:http://dotnet.mvps.org/dotnet/articles/nullabledates/>

In VB.NET 'Nothing' referes to a value type's default value. Thus you can
specify 'Nothing' as default value for the parameter. Note that this
doesn't work with VB 2002/2003, but will be supported in VB 2005. In
general I don't recommend to rely on a certain value to be interpreted as a
null value.
 
T

Terry Olsen

I have recently written an article in German about supporting nullable
date and time values. Maybe you'll find some of the code snippets useful:

The online translators don't do a very good job of turning this into english
:(
 
G

Guest

Yes MS have always had a blind spot with dates - a 'leaving date' in an
employee record needs to be blank until they leave, but even with VB6 dates
there was no equivalent of 'blank date' (which would map to NULL in SQL). I
wish they would see the light!
 
H

Herfried K. Wagner [MVP]

John Austin said:
Yes MS have always had a blind spot with dates - a 'leaving date' in an
employee record needs to be blank until they leave, but even with VB6
dates
there was no equivalent of 'blank date' (which would map to NULL in SQL).
I
wish they would see the light!

..NET's 'SqlDateTime' supports null values even in current versions of
..NET... Maybe it's suitable in your scenario.
 

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