views:

1111

answers:

13

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

Ive 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.

A: 

OK, I am unsure what you are trying to do here:

  • The code you are posting is NOT .NET, are you trying to port?
  • DateTime's cannot be declared as constants.
  • DateTime's are a data type, so once init'ed, the format that they were init'ed from is irrelevant.
  • If you need a constant value, then just create a method to always return the same DateTime.

For example:

public static DateTime SadDayForAll()
{
    return new DateTime(2001, 09, 11);
}

Update

Where the hell are you getting all that from?!

  • There are differences between C# and VB.NET, and this highlights one of them.
  • Date is not a .NET data type - DateTime is.
  • It looks like you can create DateTime constants in VB.NET but there are limitations
  • The method was there to try and help you, since you cannot create a const from a variable (i.e. optional param). That doesn't even make sense.

I found all of this within a min or two on Google. Think about that before shooting down someone who is trying to help you.

Rob Cooper
A: 

Calm down dude, I wasnt shooting you down, but merely clarifying my question based on your responses. This site wont be very helpful if guys like you are jumping down people's throats. If I would have found this on Google, I wouldnt be posting it here.

Let me clarify again: 1. I am using VB.NET. Dont care whether the answer is in C# or VB

  1. DateTimes can be declared as constants (Private Const d as DateTime = #12/31/08#)

  2. If they are being init'ed somewhere other than the US, then they cant get init'ed properly in the first place. ie. Private Const d as Date = #12/31/08# blows up in an international setting because there arent 31 months

  3. A Method, or shared readonly field doesnt help me when I want to intialize an optional parameter to that constant for use throughout my application. ie. Private Sub DoSomething(x as integer, optional y as date = MyDateConstant)

Lucas
A: 

If the answer is, "that cant be done", Im fine with that. Just curious if anyone has ideas on how to do something like this.

Lucas
A: 

According to the Microsoft documentation,

"You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy, for example #5/31/1993#. This requirement is independent of your locale and your computer's date and time format settings."

Are you saying that this is not correct and the parsing is affected by the current locale?

Edit: Did you try with a 4-digit year?

Curt Hagenlocher
Curt, as an update. I talked to our developer that was having the problem, and upon delving into it further, there was a CDate("12/31/2049") in his code that was actually the culprit.Thanks for your input and willingness to help.
Lucas
A: 

Thanks Curt.

We are seeing this line blow up in an international setting:

Private Const d as Date = #12/31/08#

Error message saying that is an invalid date. It works fine in the scenario where the locale is m/d/yy

Curt: Yes, we did.

Lucas
What version of Visual Studio are you using? The VB.net team is two floors beneath me, and I'm interested in making sure this gets fixed
Curt Hagenlocher
As an aside, I'm not able to reproduce this with Visual Studio 2008 SP1 -- though I can't swear that I'm doing the right thing to change the current locale.
Curt Hagenlocher
A: 

Ok right, I understand more where you are coming from..

How about:

  • Create a static method that returns the date constant. This overcomes the international issue since it is returned as the specific DateTime value.
  • Now I remember optional params from my VB6 days, but can you not just overload the method? If you are using the overloaded method without the date, just pull it from the static?

EDIT: If you are unsure what I mean and would like a code sample, just comment this post and I will chuck one on.

Rob Cooper
A: 
  1. DateTimes cannot be declared as constants. In .NET, only something like ints can be. Instead, you want to use static readonly.

  2. I've never seen that "#12/31/08#" syntax before; I guess it's a VB.NET thing. Instead you want to use the constructor, like e.g. static readonly DateTime test = new DateTime(2008, 09, 11). The constructor only takes things in YYYY, MM, DD order. It's the string formatting and parsing that is nationale-dependent.

  3. I guess VB.NET allows default parameters like that with things that are const; cool, did not know. But since DateTimes cannot be const, you'll have to do it with method overloading, e.g. (C#)

    private void DoSomething(int x) { DoSomething(x, MyDateConstant); }

Domenic
Did you read my previous post? It appears you can declare DateTime constants in VB.NET - there are limitations. Syntax - its MM/DD/YY? See my response just before yours, that would solve the issue.
Rob Cooper
A: 

Rob, thank you.

Yes, that is along the same lines as my orignal idea of: Public Shared Readonly D as Date = new Date(x, y, z)

Yes, we absoltely could overload the method. We could also default the optional parameter to nothing, then check if the parameter is nothing and set it to the "const". However, the reason I was asking was because I was trying to avoid a breaking change across the codebase that is using the Public Const d as Date = #somedate# as a default to an optional parameter.

Lucas
Yeah I feel your pain man, been there myself. Thats the problem with going global. At least once migrated to the new static method, you will never have a problem again :D ;)
Rob Cooper
A: 

Domenic, this is legal syntax in VB, and it is a constant

Private Const d as DateTime = #12/31/08#

Rob was right, in this case I am using VB.NET, but I want to pose the question independent of which .NET language, because you could always compile it into a dll and use it, so it wouldnt matter.

Lucas
A: 

Dumb question because I know this is a misuse of the "answer" section, but how do I comment on a post?

Lucas
Click "add comment" just below the post
Curt Hagenlocher
A: 

Once you have data into Date objects in VB, you don't have to worry about globalization until you compare something to it or try to export it.

This is fine:

Dim FirstDate as Date = Date.UtcNow() 'or this: = NewDate (2008,09,10)'
Dim SecondDate as Date

SecondDate = FirstDate.AddDays(1)

This pulls in the globalization rules and prints in the current thread's culture format:

HeaderLabel.Text = SecondDate.ToString()

This is bad:

Dim BadDate as Date = CDate("2/20/2000")

Actually--even that is OK if you force CDate in that case to use the right culture (InvariantCulture):

Dim OkButBadPracticeDate as Date = CDate("2/20/2000", CultureInfo.InvariantCulture)

If you want to force everything to a particular culture, you need to set the executing thread culture and UI culture to the desired culture (en-US, invariant, etc.).

Make sure you aren't doing any work with dates as strings--make sure they are actual Date objects!

Michael Haren
A: 

Michael,

I think that is my problem, I cant even get that date into the object, it blows up intializing the variable.

Curt,

I am using VS2005. I am not in tomorrow, but I will talk to our guy having this problem next week and see if he can create a repro scenario for you.

I dont have "Add Comment", so it must be because I am not a registered user. I am making a mess of this post though :)

Lucas
A: 

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.

Jason DeFontes
It's certainly possible that there's a parsing bug in the VB (2005) compiler. However, I wasn't able to reproduce this with the 2008 version.
Curt Hagenlocher
Thanks Jason, you are absolutely right. I checked with the developer having this problem, and sure enough there was a CDate("12/31/2049") in there.
Lucas