使用字符串变量来决定使用哪个多维数组



我有大约17个2-d数组,需要根据用户选择访问。我想用一个变量来表示这些,但是我得到了这个错误:

"Public ReadOnly默认属性Chars(index As Integer) As Char"参数太多

下面是我的代码示例:

ElseIf EcoType = "MB" Then
        If MatrixVeg.Substring(0, 2) = "CO" Then
            x = Array.IndexOf(CO, MatrixVeg)
            EcoGroup = "MBCO"
        ElseIf MatrixVeg.Substring(0, 2) = "HL" Then
            x = Array.IndexOf(HL, MatrixVeg)
            EcoGroup = "MBHL"
        ElseIf MatrixVeg.Substring(0, 2) = "OF" Then
            x = Array.IndexOf(OFF, MatrixVeg)
            EcoGroup = "MBOF"
        ElseIf MatrixVeg.Substring(0, 2) = "OW" Then
            x = Array.IndexOf(OW, MatrixVeg)
            EcoGroup = "MBOW"
        ElseIf MatrixVeg.Substring(0, 2) = "SP" Then
            x = Array.IndexOf(SP, MatrixVeg)
            EcoGroup = "MBSP"
        ElseIf MatrixVeg.Substring(0, 2) = "WC" Then
            x = Array.IndexOf(WC, MatrixVeg)
            EcoGroup = "MBWC"
        ElseIf MatrixVeg.Substring(0, 2) = "WD" Then
            x = Array.IndexOf(WD, MatrixVeg)
            EcoGroup = "MBWD"
        End If
    End If
    y = Array.IndexOf(ST, MatrixSoil)
    Ecosite = EcoGroup(y, x)
    Return Ecosite

从您的代码和注释中,似乎MCO, MBHL, MBOF等都是变量,但在您的If语句中,您分配Ecogroup 字符串值而不是变量本身。

要解决这个问题,你只需要在赋值时去掉引号(例如EcoGroup = MBCO)。

在风格上,我建议在这种情况下使用Select Case语句。看一下修改后的代码:
ElseIf EcoType = "MB" Then
    Select Case MatrixVeg.Substring(0, 2)
        Case "CO"
            x = Array.IndexOf(CO, MatrixVeg)
            EcoGroup = MBCO
        Case "HL"
            x = Array.IndexOf(HL, MatrixVeg)
            EcoGroup = MBHL
        Case "OF"
            x = Array.IndexOf(OFF, MatrixVeg)
            EcoGroup = MBOF
        Case "OW"
            x = Array.IndexOf(OW, MatrixVeg)
            EcoGroup = MBOW
        Case "SP"
            x = Array.IndexOf(SP, MatrixVeg)
            EcoGroup = MBSP
        Case "WC"
            x = Array.IndexOf(WC, MatrixVeg)
            EcoGroup = MBWC
        Case "WD"
            x = Array.IndexOf(WD, MatrixVeg)
            EcoGroup = MBWD
        End Select
End If
y = Array.IndexOf(ST, MatrixSoil)
Ecosite = EcoGroup(y, x)
Return Ecosite

相关内容

  • 没有找到相关文章

最新更新