如何同时循环多个数组?

时间:2013-08-13 10:55:44

标签: vb.net

我是vb的新手,很抱歉,如果之前我的问题已被提出过。我已经搜索过但无法找到或者认出答案。我正在使用visual studio 2010并在vb.net中创建应用程序。 我有两个名为问题和答案的数组。我想同时遍历这两个数组并从这两个数组中检索记录。我正在使用以下代码。

 Dim sql As String = "SELECT QuestionId,Answer FROM tbl_Answers"
 Dim dt As DataTable = obj.GetDTbl(sql)
 For Each row As DataRow In dt.Rows              
     SelectedAnswers += row("Answer").ToString & ","
 Next
 Dim Questions() As String = QuestionsIds.Split(",")
 For Each question In Questions
     Dim answers() As String = SelectedAnswers.Split(",")
     For Each answer In answers
         Dim rbl As RadioButtonList = DirectCast(plcHolderForm.FindControl("Question_" & question), RadioButtonList)


         rbl.SelectedIndex = rbl.Items.IndexOf(rbl.Items.FindByValue("answer".ToString))

在上面的代码中,很明显可以看出我的外部for循环只执行一次。我需要遍历每个循环2以上,以便针对每个问题,我可以得到它通过FindByValue函数重新获得的答案

1 个答案:

答案 0 :(得分:1)

你能在1个泛型for循环中循环遍历问题和答案吗?

Dim CurQuestion as String
Dim CurAnswer as String
For i As Integer = 0 To Questions.Length - 1
    CurQuestion = Questions(i)
    CurAnswer = Answers(i)
    'Do what you need what current question and answer
Next

这假设您有相同数量的问题和答案,并且它们在各自的数组中排成一行。如果没有,你必须对其进行微调以满足您的特定需求。