asp.net.net-4.0calendarglobalizationculture

Dynamically declare System.Globalization Calendars in a function?


I am playing with culture calendars. I would have thought GregorianCalendar (and the others) would be a type of under System.Globalization.Calendar, but it's not. It's a Globalization class equal to Calendar. In the function below, I don't think I can do what I want to do, but before a scrap it...

I am getting error about "cal" hides a variable in an enclosed block but I did set it to a default in the dim statement. The Calendar class does not seem to have the ability to declare and then paint it with a culture. It seems you have to declare the "cultural" calendar class and it cannot be changed. Do you see a way around this and keep it in a short function?

Public Function GetDaysInMonth(ByVal dt As Date,
                               Optional ByVal sCalendar As String = "GREGORIAN") As Integer

    Dim cal As System.Globalization.GregorianCalendar

    Dim y, m As Integer
    Select Case UCase(sCalendar)
        Case "HEBREW"
            Dim cal As New HebrewCalendar
        Case "HIJRI"
            Dim cal As New HijriCalendar
        Case "JAPENESE"
            Dim cal As New JapaneseCalendar
        Case "JULIAN"
            Dim cas As New JulianCalendar
        Case "KOREAN"
            Dim cal As New KoreanCalendar
        Case "TAIWAN"
            Dim cal As New TaiwanCalendar
        Case "THAIBUDDHIST"
            Dim cal As New ThaiBuddhistCalendar
        Case Else 'Gregorian
    End Select
    y = Year(dt)
    m = Month(dt)
    Return (cal.GetDaysInMonth(y, m))

Solution

  • SOLVED. I don't know if this is the easiest way...but here is the final code that work.

        Public Function GetDaysInMonth(ByVal dt As Date,
                                       Optional ByVal sCalendar As String = "GREGORIAN") As Integer
    System.Globalization.CultureInfo.InvariantCulture
            Dim cal As System.Globalization.GregorianCalendar
            Dim y, m As Integer
            y = Year(dt)
            m = Month(dt)
            Select Case UCase(sCalendar)
                Case "HEBREW"
                    Dim cal1 As New HebrewCalendar
                    Return (cal1.GetDaysInMonth(y, m))
                Case "HIJRI"
                    Dim cal2 As New HijriCalendar
                    Return (cal2.GetDaysInMonth(y, m))
                Case "JAPENESE"
                    Dim cal3 As New JapaneseCalendar
                    Return (cal3.GetDaysInMonth(y, m))
                Case "JULIAN"
                    Dim cal4 As New JulianCalendar
                    Return (cal4.GetDaysInMonth(y, m))
                Case "KOREAN"
                    Dim cal5 As New KoreanCalendar
                    Return (cal5.GetDaysInMonth(y, m))
                Case "TAIWAN"
                    Dim cal6 As New TaiwanCalendar
                    Return (cal6.GetDaysInMonth(y, m))
                Case "THAIBUDDHIST"
                    Dim cal7 As New ThaiBuddhistCalendar
                    Return (cal7.GetDaysInMonth(y, m))
                Case Else 'Gregorian
                    Return (cal.GetDaysInMonth(y, m))
            End Select
        End Function