Lucas Lucas - 6 months ago 34
Vb.net Question

.NET Date Const (with Globalization)

Does anyone know of a way to declare a date constant that is compatible with international dates?

I've tried:

public const ADate as Date = #12/31/04# - not international compatible


public shared readonly ADate As New Date(12, 31, 04) - breaking change if you have an optional parameter that defaults to this value because it isnt constant.

Answer

If you look at the IL generated by the statement

public const ADate as Date = #12/31/04#

You'll see this:

.field public static initonly valuetype [mscorlib]System.DateTime ADate
.custom instance void [mscorlib]System.Runtime.CompilerServices.DateTimeConstantAttribute::.ctor(int64) = ( 01 00 00 C0 2F CE E2 BC C6 08 00 00 )

Notice that the DateTimeConstantAttribute is being initialized with a constructor that takes an int64 tick count. Since this tick count is being determined at complile time, it seems unlikely that any localization is coming into play when this value is initialized at runtime. My guess is that the error is with some other date handling in your code, not the const initialization.