0

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))
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Ted
  • 123
  • 3
  • 13

2 Answers2

0

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
Ted
  • 123
  • 3
  • 13
0

Although this question has been dormant for 8 years, the following works under Visual Studio Professional 2022 (64-bit):

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

    Dim y As Integer = Year(dt)

    Dim m As Integer = Month(dt)

    Dim objCalendar As Globalization.Calendar = GetCalendar(sCalendar)

    Return (objCalendar.GetDaysInMonth(y, m))

End Function

Private Function GetCalendar(
    sCalendar As String
) As Globalization.Calendar

    Select Case UCase(sCalendar)
        Case "HEBREW"
            Return New Globalization.HebrewCalendar
        Case "HIJRI"
            Return New Globalization.HijriCalendar
        Case "JAPENESE"
            Return New Globalization.JapaneseCalendar
        Case "JULIAN"
            Return New Globalization.JulianCalendar
        Case "KOREAN"
            Return New Globalization.KoreanCalendar
        Case "TAIWAN"
            Return New Globalization.TaiwanCalendar
        Case "THAIBUDDHIST"
            Return New Globalization.ThaiBuddhistCalendar
        Case "JULIAN"
            Return New Globalization.JulianCalendar
        Case Else
            Return New Globalization.GregorianCalendar
    End Select

End Function
Paul Margus
  • 41
  • 1
  • 3