我试图定义一个数组但是在我的定义语句中它表示该变量在被赋值之前被使用。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSolve.Click
Dim txtNumber1() As Integer = {Val(Me.txt1Number1.Text), Val(Me.txt2Number1.Text), Val(Me.txt3Number1.Text), Val(Me.txt4Number1.Text), Val(Me.txt5Number1.Text), Val(Me.txt6Number1.Text), Val(Me.txt7Number1.Text), Val(Me.txt8Number1.Text), Val(Me.txt9Number1.Text), Val(Me.txt10Number1.Text), Val(Me.txt11Number1.Text), Val(Me.txt12Number1.Text), Val(Me.txt13Number1.Text), Val(Me.txt14Number1.Text), Val(Me.txt15Number1.Text), Val(Me.txt16Number1.Text), Val(Me.txt17Number1.Text), Val(Me.txt18Number1.Text), Val(Me.txt19Number1.Text), Val(Me.txt20Number1.Text)}
Dim txtNumber2() As Integer = {Val(Me.txt1Number2.Text), Val(Me.txt2Number2.Text), Val(Me.txt3Number2.Text), Val(Me.txt4Number2.Text), Val(Me.txt5Number2.Text), Val(Me.txt6Number2.Text), Val(Me.txt7Number2.Text), Val(Me.txt8Number2.Text), Val(Me.txt9Number2.Text), Val(Me.txt10Number2.Text), Val(Me.txt11Number2.Text), Val(Me.txt12Number2.Text), Val(Me.txt13Number2.Text), Val(Me.txt14Number2.Text), Val(Me.txt15Number2.Text), Val(Me.txt16Number2.Text), Val(Me.txt17Number2.Text), Val(Me.txt18Number2.Text), Val(Me.txt19Number2.Text), Val(Me.txt20Number2.Text)}
Dim txtAnswer() As Integer = {Val(Me.txt1Answer.Text), Val(Me.txt2Answer.Text), Val(Me.txt3Answer.Text), Val(Me.txt4Answer.Text), Val(Me.txt5Answer.Text), Val(Me.txt6Answer.Text), Val(Me.txt7Answer.Text), Val(Me.txt8Answer.Text), Val(Me.txt9Answer.Text), Val(Me.txt10Answer.Text), Val(Me.txt11Answer.Text), Val(Me.txt12Answer.Text), Val(Me.txt13Answer.Text), Val(Me.txt14Answer.Text), Val(Me.txt15Answer.Text), Val(Me.txt16Answer.Text), Val(Me.txt17Answer.Text), Val(Me.txt18Answer.Text), Val(Me.txt19Answer.Text), Val(Me.txt20Answer.Text)}
Dim i As Integer
Dim intRealAnswer() As Integer
For i = 0 To txtNumber1(i) - 1
intRealAnswer(i) = txtNumber1(i) - txtNumber2(i)
If txtAnswer(i) = intRealAnswer(i) Then
Me.lblAnswer1.Text = "Correct:" & intRealAnswer(i)
Else
Me.lblAnswer1.Text = "Incorrect:" & intRealAnswer(i)
End If
Next
End Sub
错误发生在intRealAnswer(i)= txtNumber1(i) - txtNumber2(i)行 谢谢你的帮助。
答案 0 :(得分:2)
虽然您已声明intRealAnswer
:
Dim intRealAnswer() As Integer
您尚未指定数组中有多少项。如果需要初始化某个值,可以像使用其他变量一样执行此操作。像这样:
Dim intRealAnswer() As Integer = {0, 0, 0, 0, ...}
或者你可以这样做来声明一个包含10个项目(索引0到9)的数组:
Dim intRealAnswer(9) As Integer
答案 1 :(得分:0)
在使用之前,您必须初始化数组或声明其大小。
答案 2 :(得分:0)
尝试为您的整数提供默认值?
Dim i As Integer = 0
Dim intRealAnswer() As Integer = {0}
答案 3 :(得分:0)
改变你的路线
Dim intRealAnswer() As Integer
到Dim intRealAnswer As Integer
由于您只存储当前值,因此没有必要成为数组的真正原因。另外注意你应该摆脱Val()并使用Integer.Parse()并在你的表单上如果有人将1.5放入文本框中你是否检查该值是否真的是一个整数?只需学习一些输入,即允许用户在一个文本框中输入1.5而在另一个文本框中输入3.0将会在结果中看到小数。
答案 4 :(得分:0)
我建议使用IEnumerable对象,如List(Of Integer)和
Dim txtNumbers as New List(Of Integer) From {Val(Me.txt1Number1.Text), Val(Me.txt2Number1.Text), ...}
txtNumbers.Add(Val(Me.txt3Number1.Text))
甚至你可以逐个“Add
”或只是“AddRange
”。广告素材使用“For Each Item as Control in Me.Controls
”使列表成为可能。