将String转换为其整数类型赋值

时间:2017-10-02 02:05:27

标签: vb.net

在VB.net中使用下面的代码,我有一些变量(HUCROW1,HUCROW2,HUCROW3),它们是整数类型,并为每个变量分配一个特定的值。 然后在循环中我尝试使HUCROW等于上面的变量。 HUCROW是字符串类型,我将它与循环编号连接起来,例如使用" HUCROW" &安培; 1。

                HUCROW1 = 66                
                HUCROW2 = 84                
                HUCROW3 = 102                
                HUCROW4 = 120


        For j = 1 To 3                
            Dim HUCROW As String = "HUCROW" & (j)


            If ScheduleSht.Cells(HUCROW, 9).Value.Ticks <> Nothing Then
                HUCdates(i) = ScheduleSht.Cells(HUCROW, 9).value.Ticks
                HUCdates(i) = ScheduleSht.Cells(HUCROW, 9).value.Ticks
            Else
                HUCdates(i) = 0
            End If

        Next

我的目标是获得66的HUCROW1值,但它没有工作并且给出了#34;类型不匹配错误&#34;在ScheduleSht.Cells(HUCROW,9).value.Ticks,因为它没有将字符串HUCROW转换为其指定的数字。 有关如何转换字符串类型的任何帮助&#34; HUCROW1&#34;可以理解为指定的整数值为HUCROW1。

1 个答案:

答案 0 :(得分:0)

我认为使用数组或字典存储和检索值更容易。

数组样本(减去j,因为数组的索引从零开始)

Dim HUCROWS As Integer() = {66, 84, 102, 120}

For j = 1 To 3
    Console.WriteLine(HUCROWS(j - 1))
Next

字典示例

Dim HUCROWS As New Dictionary(Of String, Integer) From {
    {"HUCROW1", 66},
    {"HUCROW2", 84},
    {"HUCROW3", 102},
    {"HUCROW4", 120}
}

For j = 1 To 3
    Dim HUCROW As String = "HUCROW" & j
    Console.WriteLine(HUCROWS(HUCROW))
Next

两个代码的结果

66
84
102