Optional Date Parameters 
Author Message
 Optional Date Parameters

Why is a date constant invalid when declaring a Optional parameter
with a type of date?

I can put a string value here, but the problem I can foresee is that
what date this will produce is dependent on the location at which it's
compiled e.g.

Function Fred(Optional ByVal MyDate As Date = "12/01/2001")
    ....
End Function

If compiled in the UK the default will be 12th January whereas if
compiled in the US the default will be 1st December - unless someone
can tell me different. At least if it was a date constant we'd *know*
that the date was in US format.

Regards

Paul Hatcher
Principal Consultant
Component Software



Mon, 09 Aug 2004 20:40:00 GMT  
 Optional Date Parameters

Quote:
> Why is a date constant invalid when declaring a Optional
> parameter  with a type of date?

> I can put a string value here, but the problem I can foresee is
> that  what date this will produce is dependent on the location
> at which it's  compiled e.g.

> Function Fred(Optional ByVal MyDate As Date = "12/01/2001")
>     ....
> End Function

> If compiled in the UK the default will be 12th January whereas if
> compiled in the US the default will be 1st December - unless
> someone  can tell me different. At least if it was a date
> constant we'd *know*  that the date was in US format.

This probably only works with Option Strict Off?

You should use a date literal:

Function Fred(Optional ByVal MyDate As Date = #12/01/2001#)

Armin



Mon, 09 Aug 2004 21:44:59 GMT  
 Optional Date Parameters
I think you're correct you can only use a date literal with Option
Strict Off - which is completely back to front!

Try it with Strict On and see if you have to use German date syntax
i.e. "01.12.2001"?

Regards

Paul Hatcher
Principal Consultant
Component Software



Mon, 09 Aug 2004 22:23:00 GMT  
 Optional Date Parameters

Quote:
> I think you're correct you can only use a date literal with
> Option  Strict Off - which is completely back to front!

I'm not at my vb.net machine but I thought it's the other way round:
You have to use a Date type as the default value with Option Strict
On. You *can* use a string as the default value with Option Strict
Off.

Quote:
> Try it with Strict On and see if you have to use German date
> syntax  i.e. "01.12.2001"?

I don't think the expression is evaluated at compile time. It's an
assignment of a string to a Date argument. This should cause an error
at compile time With Option Strict On, doesn't it? I'll try when I'm
at my vb.net machine...

Armin



Tue, 10 Aug 2004 00:00:01 GMT  
 Optional Date Parameters
Weird, working in another project and it's 'gone away', i.e. it now
wants a Date constant rather than telling me that using one is
invalid.

Sounds like the compiler can occasional get confused - I know I am :-)

Regards

Paul Hatcher
Principal Consultant
Component Software



Tue, 10 Aug 2004 01:26:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Optional Date Parameter Bug!

2. Using Parameter Queries with Optional Parameters

3. passing optional parameters to the parameters collection

4. Writing A Function with optional parameters

5. how to declare a function with optional parameters

6. > passing vb6 optional byref parameter into c#

7. how to make array an optional parameter

8. Function overloading and Optional parameters

9. Inheritance across VB assemblies: Optional parameter compiler problem

10. Default value in optional parameter

11. C# optional parameters

12. Defaultvalue of Optional Struct Parameters

 

 
Powered by phpBB® Forum Software