0

我正在开发一个 vb.net 2010 表单,它将添加用户输入。我想制作一个可以计算总和、平均值、方差和标准差的程序....任何人都可以帮助添加代码和解决问题????请帮助我......我正在尝试解决问题,但是当我运行程序时出现错误“InvalidOperationException 未处理(创建表单时发生错误。有关详细信息,请参阅 Exception.InnerException。错误是:未设置对象引用到一个对象的一个​​实例。)”

任何人都可以帮助我解决这个问题吗?

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
4

1 回答 1

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
于 2013-02-08T11:00:39.303 回答