在If语句中使用Right函数会导致语法错误

时间:2012-03-09 03:38:50

标签: vb.net reportbuilder3.0

我在Report Builder 3.0(例如20124,20125)的报表中有一个多值单字符串作为参数字段。现在我想拆分这个字符串,并显示“Summer 2012”而不是“20124”和“Fall 2012”而不是“20125。我使用的是Visual Basic。这是我在Report Builder 3.0自定义代码中创建的两个函数。错误是在“If Right(yearterm)...”行的第二个函数中。它给了我一个语法错误。如果我写“String.Right”它给了我错误:“'对'不是一个'String'的成员。如果我拿出整个“If声明”报告将会运行,但“20124,20125”给了我:“,2012,2012”。

我该如何做到这一点?

Public Function SplitParameterValues(ByVal parameter As Parameter) As String
    Dim result As String
    Dim a(0 To 10) As String
    a = Split(parameter.Value, ",")

    For i As Integer = 0 to a.length - 1
        result = result +", "  + YearTermTranslation(a(i))
    Next

    Return result
End Function

Public Function YearTermTranslation(ByVal yearterm As String) As String
    Dim result As String
    Dim term As String
    Dim year = Left(yearterm, 4)

    If Right(yearterm, 1) = 5
    Then term = "Fall"
    Else If Right(yearterm, 1) = 4
    Then term = "Summer"
    Else If Right(yearterm, 1) = 3
    Then term = "Spring"
    Else term = "Winter"
    End If

    result = term + " " + year

    Return result
End Function

1 个答案:

答案 0 :(得分:2)

问题在于,您将Then放在不同的行上,以及Else中的语法错误;它与Right无关 - 除非这是一个复制粘贴错误。

Public Function YearTermTranslation(ByVal yearterm As String) As String
    Dim result As String
    Dim term As String
    Dim year = Left(yearterm, 4)

    If Right(yearterm, 1) = 5 Then
        term = "Fall"
    ElseIf Right(yearterm, 1) = 4 Then
        term = "Summer"
    ElseIf Right(yearterm, 1) = 3 Then
        term = "Spring"
    Else
        term = "Winter"
    End If

    result = term + " " + year

    Return result
End Function

哦,如果这实际上是VB.NET ...请学习VB.NET。

Public Function SplitParameterValues(ByVal parameter As Parameter) As String
    Dim result As String = String.Empty
    Dim a() As String = parameter.Value.Split(","c)

    For i As Integer = 0 To a.length - 1
        result &= ", " & YearTermTranslation(a(i))
    Next

    Return result
End Function

Public Function YearTermTranslation(ByVal yearterm As String) As String
    Dim term As String
    Dim year As String = yearterm.Substring(0, 4)

    Select Case yearterm(yearterm.Length - 1)
        Case "5"c
            term = "Fall"
        Case "4"c
            term = "Summer"
        Case "3"c
            term = "Spring"
        Case Else
            term = "Winter"
    End Select

    Return term & " " & year
End Function