在函数中动态声明System.Globalization Calendars?

时间:2013-07-02 09:22:44

标签: asp.net .net-4.0 calendar globalization culture

我正在玩文化日历。我原以为GregorianCalendar(和其他人)会是System.Globalization.Calendar下的一种类型,但事实并非如此。它是一个等于Calendar的Globalization类。在下面的函数中,我认为我不能做我想做的事情,但在废弃它之前......

我收到关于“cal”的错误隐藏了一个封闭块中的变量,但我确实在dim语句中将其设置为默认值。 Calendar类似乎没有能力声明,然后用文化绘制它。您似乎必须声明“文化”日历类,并且无法更改。你是否看到了解决这个问题并将其保留在一个简短的功能中?

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))

1 个答案:

答案 0 :(得分:0)

解决。我不知道这是否是最简单的方法......但这是最终的代码。

    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