'答案2'没有声明。由于保护级别

时间:2015-10-04 13:39:29

标签: vb.net variables

我需要为幼儿创建一个数学测验,但是我希望我的测验在输入计算出的答案时显示答案,尽管当我尝试运行程序时它仍然说我的变量尚未声明。

Public Class Form2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim RN As New Random
    Dim RandomNum1 = RN.Next(1, 5)
    Dim RandomNum2 = RN.Next(5, 10)
    Dim RandomNum3 = RN.Next(15, 20)
    Dim RandomNum4 = RN.Next(25, 30)
    Dim RandomNum5 = RN.Next(35, 40)
    Dim arethmeticsymbollist As New List(Of String)
    Dim Symbol1 = "+"
    Dim Symbol2 = "-"
    Dim Symbol3 = "*"
    Dim Number1 As Integer
    Dim Number2 As Integer
    Dim Number3 As Integer
    Dim Number4 As Integer
    Dim Number5 As Integer
    Dim Number6 As Integer
    Dim Number7 As Integer
    Dim Number8 As Integer
    Dim Number9 As Integer
    Dim Number10 As Integer
    Dim Number11 As Integer
    Dim Number12 As Integer
    Dim Number13 As Integer
    Dim Number14 As Integer
    Dim Number15 As Integer
    Dim Number16 As Integer
    Dim Number17 As Integer
    Dim Number18 As Integer
    Dim Number19 As Integer
    Dim Number20 As Integer
    Dim Answer1 As Integer
    Dim Answer2 As Integer
    Dim Answer3 As Integer
    Dim Answer4 As Integer
    Dim Answer5 As Integer
    Dim Answer6 As Integer
    Dim Answer7 As Integer
    Dim Answer8 As Integer
    Dim Answer9 As Integer
    Dim Answer10 As Integer
    Dim TotalBox As Integer
    Dim TotalCorrect As Integer
    Dim TotalIncorrect As New Integer


    RandomInteger1.Text = RandomNum1
    RandomInteger2.Text = RandomNum2
    RandomInteger3.Text = RandomNum3
    RandomInteger4.Text = RandomNum4
    RandomInteger5.Text = RandomNum5
    RandomInteger6.Text = RandomNum1
    RandomInteger7.Text = RandomNum2
    RandomInteger8.Text = RandomNum3
    RandomInteger9.Text = RandomNum4
    RandomInteger10.Text = RandomNum5
    RandomInteger11.Text = RandomNum1
    RandomInteger12.Text = RandomNum2
    RandomInteger13.Text = RandomNum3
    RandomInteger14.Text = RandomNum4
    RandomInteger15.Text = RandomNum5
    RandomInteger16.Text = RandomNum1
    RandomInteger17.Text = RandomNum2
    RandomInteger18.Text = RandomNum3
    RandomInteger19.Text = RandomNum4
    RandomInteger20.Text = RandomNum5

    RandomSymbol1.Text = Symbol1
    RandomSymbol2.Text = Symbol2
    RandomSymbol3.Text = Symbol3
    RandomSymbol4.Text = Symbol1
    RandomSymbol5.Text = Symbol2
    RandomSymbol6.Text = Symbol3
    RandomSymbol7.Text = Symbol1
    RandomSymbol8.Text = Symbol2
    RandomSymbol9.Text = Symbol3
    RandomSymbol10.Text = Symbol1
    Number1 = Integer.Parse(RandomInteger1.Text())
    Number2 = Integer.Parse(RandomInteger2.Text())
    Answer1 = Number1 + Number2
    Number3 = Integer.Parse(RandomInteger3.Text())
    Number4 = Integer.Parse(RandomInteger4.Text())
    Answer2 = Number3 - Number4
    Number5 = Integer.Parse(RandomInteger5.Text())
    Number6 = Integer.Parse(RandomInteger6.Text())
    Answer3 = Number5 * Number6
    Number7 = Integer.Parse(RandomInteger7.Text())
    Number8 = Integer.Parse(RandomInteger8.Text())
    Answer4 = Number7 + Number8
    Number9 = Integer.Parse(RandomInteger9.Text())
    Number10 = Integer.Parse(RandomInteger10.Text())
    Answer5 = Number9 - Number10
    Number11 = Integer.Parse(RandomInteger11.Text())
    Number12 = Integer.Parse(RandomInteger12.Text())
    Answer6 = Number11 * Number12
    Number13 = Integer.Parse(RandomInteger13.Text())
    Number14 = Integer.Parse(RandomInteger14.Text())
    Answer7 = Number13 + Number14
    Number15 = Integer.Parse(RandomInteger15.Text())
    Number16 = Integer.Parse(RandomInteger16.Text())
    Answer8 = Number13 - Number14
    Number17 = Integer.Parse(RandomInteger17.Text())
    Number18 = Integer.Parse(RandomInteger18.Text())
    Answer9 = Number18 * Number19
    Number19 = Integer.Parse(RandomInteger13.Text())
    Number20 = Integer.Parse(RandomInteger14.Text())
    Answer10 = Number19 + Number20
End Sub

Private Sub RandomInteger7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RandomInteger7.TextChanged

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton3.Click

End Sub

Private Sub SubmitButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton1.Click

End Sub

Private Sub SubmitButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton2.Click
    If Integer.Parse(AnswerBox2.Text()) = Answer2 Then CorI2.Text = "Correct" Else CorI2.Text = "Incorrect, the answer is " & Answer2
    If Integer.Parse(AnswerBox2.Text()) = Answer2 Then TotalCorrect = TotalCorrect + 1
    TotalBox.Text = TotalCorrect


End Sub

结束班

1 个答案:

答案 0 :(得分:0)

在方法中声明的本地(例如方法Number2中的Form2_Load)只能在该方法中访问。 1

因此SubmitButton2_Click无法找到Answer2,因为Answer2是不同方法中的本地。

您需要在包含类的类字段(或属性)的多个方法和函数中创建要使用的变量。

1 除非你当然使用lambdas创建一个闭包:但是这里没有。