我正在开发一个将添加用户输入的vb.net 2010表单。我想制作一个可以计算总和,平均值,方差和标准偏差的程序....任何人都可以帮助添加代码并修复问题????请帮助我....我正在尝试解决问题但是当我运行程序时出现错误" InvalidOperationException未处理(创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误is:对象引用未设置为对象的实例。)"
任何人都可以帮我解决这个问题吗?
Public Class Form1
Dim sum As Integer
Dim mean As Double
Dim x(ListBox1.Items.Count - 1) As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim TotalN As Double = InputBox("The total Number", "Enter a number")
Dim sum As Single
Dim b As Integer = ListBox1.Items.Count - 1
Dim counter As Integer
For counter = 0 To b
x(counter) = ListBox1.Items(counter)
If (Val(x(counter)) = TotalN) Then
sum = (sum + x(counter)) / TotalN
End If
Next
End Sub
End Class
答案 0 :(得分:1)
我认为你得到了异常,因为当ListBox不包含任何项时,x数组是在程序/表单的开头创建的。
尝试在ReDim
方法中执行数组的Button1_Click
。
Public Class Form1
Dim sum As Integer
Dim mean As Double
Dim x(ListBox1.Items.Count - 1) As Integer
Private Sub Button1_Click() Handles Button1.Click
Dim TotalN As Double = InputBox("The total Number", "Enter a number")
Dim sum As Single
Dim b As Integer = ListBox1.Items.Count - 1
ReDim x(ListBox1.Items.Count - 1) ' change the size of the array
Dim counter As Integer
For counter = 0 To b
x(counter) = ListBox1.Items(counter)
If (Val(x(counter)) = TotalN) Then
sum = (sum + x(counter)) / TotalN
End If
Next
End Sub
End Class