Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 每个项目的循环通过数组_Arrays_Vb.net_Loops - Fatal编程技术网

Arrays 每个项目的循环通过数组

Arrays 每个项目的循环通过数组,arrays,vb.net,loops,Arrays,Vb.net,Loops,我的ScheduleType_ID存储在如下数据库中:1~2~3~等等。 我需要能够访问每个数字(始终从0-9),以便在WinForm上执行某些操作。有一个checkedlistbox控件,这些数字表示需要选中的选项。我下面的代码并不能让我达到目的 While myReader.Read If Not String.IsNullOrEmpty(myReader("SchType_ID").ToString) Then SchedArray = (myReader("SchT

我的ScheduleType_ID存储在如下数据库中:1~2~3~等等。 我需要能够访问每个数字(始终从0-9),以便在WinForm上执行某些操作。有一个checkedlistbox控件,这些数字表示需要选中的选项。我下面的代码并不能让我达到目的

 While myReader.Read
     If Not String.IsNullOrEmpty(myReader("SchType_ID").ToString) Then
     SchedArray = (myReader("SchType_ID").ToString).Split("~")
         For i = 0 To SchedArray.Length - 2
            builder.Append(SchedArray(i) & ",")
         Next
     End If
 End While

这应该可以在没有阵列的情况下完成…

 Dim intcount As Integer = 0 'Used to make sure commas are in order
 For Each item As Integer In myReader("SchType_ID").ToString.Split("~")
    If intcount >= 1 Then
      builder.Append(",")
      builder.Append(item.ToString)
    Else
      builder.Append(item.ToString)
      intcount += 1
    End If
 Next
将项目添加到数组的另一个示例

 Dim strArray As New List(Of Integer)
 Dim intcount As Integer = 0 'Used to make sure commas are in order

    'Add items to the array
    For Each item As Integer In myReader("SchType_ID").ToString.Split("~")
        strArray.Add(item)
    Next

    'Add each item to the string builder
    For Each intItem As Integer In strArray
        If intcount >= 1 Then
            builder.Append(",")
            builder.Append(intItem.ToString)
        Else
            builder.Append(intItem.ToString)
            intcount += 1
        End If
    Next

    MessageBox.Show(builder.ToString) 'Only for testing purposes
这里还有一个函数供您使用

 Public Function BuildArray(ByVal strItems As String) As List(Of Integer)
    Dim lstArray As New List(Of Integer)

    For Each item As Integer In strItems.Split("~")
        lstArray.Add(item)
    Next

    Return lstArray
 End Function
*要使用该函数,您可以这样使用它

    Dim lstArray As List(Of Integer)
    Dim intcount As Integer = 0

    lstArray = New List(Of Integer)(BuildArray(myReader("SchType_ID").ToString))

    'Add each item to the string builder
    For Each intItem As Integer In lstArray
        If intcount >= 1 Then
            builder.Append(",")
            builder.Append(intItem.ToString)
        Else
            builder.Append(intItem.ToString)
            intcount += 1
        End If
    Next

    MessageBox.Show(builder.ToString) 'Testing purpose only
快乐编码