(Visual Basic)当我(大概)声明它们时,声明变量是未声明的

时间:2014-10-28 05:48:49

标签: vb.net visual-studio

我发现了许多与我类似的问题,但很少有简单的英语解释。我试图声明一些类级变量来作为参数提供给它们下面的多个函数,但是我得到一个" null"各种错误(来自我在网上发现的)。更具体地说,我在文本框输入中声明了我使用CDbl()转换为double的变量,因此我可以在计算这些函数时使用它们。

特定的错误消息是" NullReferenceExpectation未被usercode"处理。

我发现的消息来源暗示我的问题是这些变量必须在方法中声明,否则视觉工作室会翻转。我最初尝试这样做,但是在这种情况下,我需要从类级别变量中提供这些函数所需的参数,至少就我所知道而言....

我没有那么多寻找我的代码的正确版本,但更多的是要真正理解为什么这个"异常" (我认为是错误的行话)我一直在看。我也有完整的代码供参考,即使我还没有完成清理输出。

正如你所知,我对visual basic相当新,但是我对C ++,PHP和SQL非常不错;但由于某些原因,VB真的让我旋转。抱歉,我无法发布问题的照片,显然我在这个网站上没有足够的积分来这样做。

Public Class Form1

'declare textboxes as doubles
Dim expectedLoad As Double = CDbl(txtLoad.Text)
Dim length As Double = CDbl(txtLoad.Text)

Private Function test1(ByVal load As Double, length As Double, ByVal width As Double, ByVal area As Double)
    'Determine buckling load
    Dim bucklingLoad As Double = (0.3 * 1700000 * 3.14 * (width / 2)) / ((length / width) * (length / width))

    'return true is buckling load test is true
    If (bucklingLoad > load) Then
        test1 = True
    Else
        test1 = False
    End If

End Function


Private Function test2(ByVal load As Double, ByVal area As Double) As Boolean
    'Determine compressive stress
    Dim compressiveStress As Double = area * 445

    'return true if test is passed
    If (compressiveStress > expectedLoad) Then
        test2 = True
    Else
        test2 = False
    End If
End Function


Private Function test3(ByVal length As Double, ByVal width As Double) As Boolean
    'determine slenderness limits
    Dim slendernessLimits As Double = (length / width)

    If (slendernessLimits <= 50) Then
        test3 = True
    Else
        test3 = False
    End If
End Function


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'list out column titles and alignments


    'do and list out test results for width 2
    'buckling load
    If (test1(expectedLoad, length, 2, (3.14 * ((2 / 2) * (2 / 2)))) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If

    'compressive stress
    If (test2(expectedLoad, (3.14 * ((2 / 2) * (2 / 2)))) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If

    'slenderness test
    If (test3(length, 2) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If


    'do and list out test results for width 4
    'buckling load
    If (test1(expectedLoad, length, 2, (3.14 * ((4 / 2) * (4 / 2)))) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If

    'compressive stress
    If (test2(expectedLoad, (3.14 * ((4 / 2) * (4 / 2)))) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If

    'slenderness test
    If (test3(length, 4) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If


    'do and list out test results for width 6
    'buckling load
    If (test1(expectedLoad, length, 2, (3.14 * ((6 / 2) * (6 / 2)))) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If

    'compressive stress
    If (test2(expectedLoad, (3.14 * ((6 / 2) * (6 / 2)))) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If

    'slenderness test
    If (test3(length, 6) = True) Then
        lstOut.Items.Add("True")
    Else
        lstOut.Items.Add("False")
    End If


End Sub
End Class

1 个答案:

答案 0 :(得分:2)

首先,我将这些类 - 实例变量(expectedLoadlength)声明为privatepublic(而不是Dim)。此外,您的txtLoad对象在实例化Form1类时可能尚未初始化。

尝试一些更强大的功能,例如:

Private ReadOnly Property ExpectedLoad As Double
    Get
        If txtLoad Is Nothing Then Throw New ArgumentNullException("txtLoad", "txtLoad was null")
        Dim result As Double = 0
        If Double.TryParse(txtLoad.Text, result) = True Then
            Return result
        Else
            Throw New Exception("txtLoad.Text could not be converted to type Double")
        End If
    End Get
End Property