BC30451 VisualBasic错误

时间:2017-01-17 13:16:32

标签: vb.net function calculator

我试图在VisualBasic中创建一个计算器,但它会向我抛出这个错误。用函数'替换CastNumbers()。代码修复了一些错误,但我仍然很好奇如何解决这个问题。我无法在互联网上找到任何我能理解的相关答案,因为这是我2年来的第一个编程项目(我开始在CSharp做一些事情,但我停止了。)

Public Class Form1

    Private Sub b_ClickMe_Click(sender As Object, e As EventArgs) Handles b_ClickMe.Click
        MyTextBox.Text = "Hello"
        '---Dim firstNum As Integer = CInt(number1.Text)
        '---Dim secondNum As Integer = CInt(number2.Text)
        '---sum.Text = firstNum + secondNum
    End Sub

    Public Function CastNumbers()
    '//--- Converts input to integer, casts as firstNum and secondNum
        Dim firstNum As Integer = CInt(number1.Text)
        Dim secondNum As Integer = CInt(number2.Text)
    End Function

    Private Sub b_plus_Click(sender As Object, e As EventArgs) Handles b_plus.Click
        CastNumbers()
        sum.Text = firstNum + secondNum
    End Sub

    Private Sub b_minus_Click(sender As Object, e As EventArgs) Handles b_minus.Click
        CastNumbers()
        sum.Text = firstNum - secondNum
    End Sub

    Private Sub b_multiply_Click(sender As Object, e As EventArgs) Handles b_multiply.Click
        CastNumbers()
        sum.Text = firstNum * secondNum
    End Sub

    Private Sub b_divide_Click(sender As Object, e As EventArgs) Handles b_divide.Click
        CastNumbers()
        sum.Text = firstNum / secondNum
    End Sub

End Class

2 个答案:

答案 0 :(得分:0)

您已在函数中定义了变量,并尝试在该函数之外使用它们。这就是问题所在。

试试这个,

Public Class Form1
         Dim firstNum As Integer
         Dim secondNum As Integer
    Private Sub b_ClickMe_Click(sender As Object, e As EventArgs) Handles b_ClickMe.Click

        MyTextBox.Text = "Hello"

        'Dim firstNum As Integer = CInt(number1.Text)

        'Dim secondNum As Integer = CInt(number2.Text)

        'sum.Text = firstNum + secondNum

    End Sub

    Public Function CastNumbers() 'Converts input to integer, casts as firstNum and secondNum

        firstNum = CInt(number1.Text)

        secondNum = CInt(number2.Text)

    End Function

    Private Sub b_plus_Click(sender As Object, e As EventArgs) Handles b_plus.Click

        CastNumbers()

        sum.Text = firstNum + secondNum

    End Sub

    Private Sub b_minus_Click(sender As Object, e As EventArgs) Handles b_minus.Click

        CastNumbers()

        sum.Text = firstNum - secondNum

    End Sub

    Private Sub b_multiply_Click(sender As Object, e As EventArgs) Handles b_multiply.Click

        CastNumbers()

        sum.Text = firstNum * secondNum

    End Sub

    Private Sub b_divide_Click(sender As Object, e As EventArgs) Handles b_divide.Click

        CastNumbers()

        sum.Text = firstNum / secondNum

    End Sub

End Class

答案 1 :(得分:0)

谢谢大家的帮助。代码现在看起来像这样。

Public Class Form1

    Dim firstNum As Integer

    Dim secondNum As Integer

    Private Sub b_ClickMe_Click(sender As Object, e As EventArgs) Handles b_ClickMe.Click

        MyTextBox.Text = "Hello"

    End Sub

    Public Function ReadNumbers() 'Converts input to integer, casts as firstNum and secondNum

        firstNum = CInt(number1.Text)

        secondNum = CInt(number2.Text)

    End Function

    Private Sub b_plus_Click(sender As Object, e As EventArgs) Handles b_plus.Click

        ReadNumbers()

        sum.Text = firstNum + secondNum

    End Sub

    Private Sub b_minus_Click(sender As Object, e As EventArgs) Handles b_minus.Click

        ReadNumbers()

        sum.Text = firstNum - secondNum

    End Sub

    Private Sub b_multiply_Click(sender As Object, e As EventArgs) Handles b_multiply.Click

        ReadNumbers()

        sum.Text = firstNum * secondNum

    End Sub

    Private Sub b_divide_Click(sender As Object, e As EventArgs) Handles b_divide.Click

        ReadNumbers()

        sum.Text = firstNum / secondNum

    End Sub

End Class