1

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

 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 函数检索的答案

4

1 回答 1

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

这假设您有相同数量的问题和答案,并且它们在各自的数组中排列。如果不是,您将不得不对其进行微调以满足您的特定需求。

于 2013-08-13T11:36:23.773 回答