2015-08-13 29 views
0

我對如何編寫此代碼繪製空白。這是我現在所擁有的:根據字符串值修改程序流程

If queststg = "Q1" Then 
    q1a = vanswer 
End If 
If queststg = "Q2" Then 
    q2a = vanswer 
End If 

依此類推,直到我達到Q12。我可以弄清楚如何讓Q#(Q和數字)發生變化,但我無法弄清楚如何讓q#a發生變化。 q#一個變量名將始終是Q#加上a。

僅供參考 - 我有一個表Q1,Q2,...,Q12。 q#a告訴它將值vanswer(字段Q#)放在哪裏。上面的代碼有效,但我有12個問題,所以我不得不重複12次。必須有一個更簡單的方法。

爲了澄清什麼,我試圖做的,這裏的所有代碼(我只包括3個問題,以節省空間) -

Protected Sub btnGetResult_Click(ByVal sender As Object, ByVal e As EventArgs) 
    Dim sb As New StringBuilder() 
    Dim commandtext As String 
    Dim q1a, q2a, q3a As Integer 

    Dim questno As String = 1 'first question number for field name 

    For Each item As RepeaterItem In Repeater1.Items 

     'find question 
     Dim lblquestion As Label = DirectCast(item.FindControl("lblQuestion"), Label) 

     Dim vanswer As String = String.Empty 

     'Count q for question number 

     Dim queststg As String = "Q" & questno 

     'Using FindControl method to find the radiobuttons 
     Dim rbnStronglyAgree As RadioButton = DirectCast(item.FindControl("rbnStronglyAgree"), RadioButton) 
     Dim rbnAgree As RadioButton = DirectCast(item.FindControl("rbnAgree"), RadioButton) 
     Dim rbnNeutral As RadioButton = DirectCast(item.FindControl("rbnNeutral"), RadioButton) 
     Dim rbnDisagree As RadioButton = DirectCast(item.FindControl("rbnDisagree"), RadioButton) 
     Dim rbnStronglyDisagree As RadioButton = DirectCast(item.FindControl("rbnStronglyDisagree"), RadioButton) 

     'Check which radiobutton is checked 
     If rbnStronglyAgree.Checked Then 
      Dim hid1 As HiddenField = DirectCast(item.FindControl("HiddenField1"), HiddenField) 
      vanswer = hid1.Value 
     ElseIf rbnAgree.Checked Then 
      Dim hid2 As HiddenField = DirectCast(item.FindControl("HiddenField2"), HiddenField) 
      vanswer = hid2.Value 
     ElseIf rbnNeutral.Checked Then 
      Dim hid3 As HiddenField = DirectCast(item.FindControl("HiddenField3"), HiddenField) 
      vanswer = hid3.Value 
     ElseIf rbnDisagree.Checked Then 
      Dim hid4 As HiddenField = DirectCast(item.FindControl("HiddenField4"), HiddenField) 
      vanswer = hid4.Value 
     ElseIf rbnStronglyDisagree.Checked Then 
      Dim hid5 As HiddenField = DirectCast(item.FindControl("HiddenField5"), HiddenField) 
      vanswer = hid5.Value 
     End If 

     sb.AppendLine(String.Format("<br />Q: {0} <br />A: {1} <br />", lblquestion.Text, vanswer)) 


     **If queststg = "Q1" Then 
      q1a = vanswer 
     End If 
     If queststg = "Q2" Then 
      q2a = vanswer 
     End If 
     If queststg = "Q3" Then 
      q3a = vanswer 
     End If** 

     questno = questno + 1 
    Next 

    Using sqlconnection As New SqlConnection("Data Source=WillSQL\ict2;Initial Catalog=StudCourseEval;Integrated Security=SSPI") 

     commandtext = "Insert into tblQuestionnaire (Q1, Q2, Q3) values (@vq1a, @vq2a, @vq3a)" 

     Dim cmd As New SqlCommand(commandtext, sqlconnection) 

     sqlconnection.Open() 

     cmd.Parameters.AddWithValue("@vq1a", q1a) 
     cmd.Parameters.AddWithValue("@vq2a", q2a) 
     cmd.Parameters.AddWithValue("@vq3a", q3a) 

     cmd.ExecuteNonQuery() 

     sqlconnection.Close() 

    End Using 

    lblMSG.Text = sb.ToString() 
End Sub 

預先感謝您的幫助!

+0

你需要比這更簡單的方法? –

+0

爲什麼不使用數組或字典? – GSerg

+0

這是什麼類型的應用程序?你能提供更多關於預期結果的信息嗎? – alybaba726

回答

4

說實話,我不覺得有困難,重複這12次並沒有那麼多。但是對於它的價值,你可以將它存儲在字典中:

Dim qDict = new Dictionary(Of String, String) 
qDict.Add("Q1", q1a) 
qDict.Add("Q2", q2a) 
' .... 
qDict.Add("Q12", q12a) 

你只需要初始化一次。然後,你可以隨時通過鍵來訪問它:

qDict(queststg) = vanswer 
0
Dim answers(11) As String ' should this be an integer? Other? It's not clear from the question 
Dim QuestionIndex As Integer = Integer.Parse(queststg.SubString(1)) - 1 
answers(QuestionIndex) = vanswer 
0

採用了矩陣,即。 QA(12,2)。然後,您可以循環使用循環索引來引用每個問題和答案。

For i = 1 to 12 
    If queststg = QA(i,1) Then 
     q1a = QA(i,2) 
    End If 
Next 
0

如何

Select Case queststg 
Case "Q1" 
q1a - vanswer 
Case "Q2" 
q2a = vanswer 
end select 
0

我可能已經錯過了你的問題點完全是我沒有找到它說清楚,但我認爲你有問題和答案的列表;

所以你應該考慮有一個問題和答案位於相同的數據結構,例如一類:

Public Class QA 
    Public Property Question As String 
    Public Property Answer As String 
    Public Sub New(question As String, answer As String) 
     Me.Question = question 
     Me.Answer = answer 
    End Sub 
End Class 

然後,您可以創建問題和答案的列表,像這樣:

Dim qaList As New List(of QA) 
qaList.Add(New QA("This is question 1", "This is the answer to question 1")) 
'etc. 

然後你就可以通過索引

For i as Integer = 0 to qaList.Count -1 
    Debug.Writeline("Question #" & i.ToString() & " is " & 
     qaList(i).Question & " the answer is " & qaList(i).Answer) 
Next 

這應該讓您的生活訪問此列表更簡單很多

+0

我認爲這可能是正確的 - 我只是很難將其放入我的代碼中。我要編輯我的問題以包含整個代碼。你可以看看嗎?謝謝! (我知道只要輸入12次並不難,但如果有100個問題呢?)感謝您的幫助! – user3033348

0

儘管我更喜歡@TimSchmelter的答案,但有人應該提到標準方法t Ø處理這樣的事情在VB與Select Case結構:

Select Case queststg 
     Case "Q1" 
      q1a = vanswer 
     Case "Q2" 
      q1b = vanswer 
     Case "Q3" 
      q1c = vanswer 
    End Select