如何访问不同子中的不同变量?

时间:2014-02-08 17:55:34

标签: input integer visual-studio-2013

我一直在尝试在Visual Studio 2012 Ultimate上制作计算器。我一直在尝试创建不同的按钮,一个要求第一个号码,第二个要求第二个号码,第三个要求操作员,最后一个在消息框中显示答案。但每当我尝试从另一个子调用Integer时,它就不起作用。我试过让子公开。这是我的代码:

Public Class Form1

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim prompt1, title1 As String
        Dim a As Integer
        prompt1 = "Enter First Number : "
        title1 = "First Number"
        a = InputBox(prompt1, title1)
    End Sub

    Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim prompt2, title2 As String
        Dim b As Integer
        prompt2 = "Enter Second Number : "
        title2 = "Second Number"
        b = InputBox(prompt2, title2)
    End Sub

    Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim prompt3, title3 As String
        Dim op As String
        Dim ans As Integer
        prompt3 = "Enter Operator : "
        title3 = "Operator"
        op = InputBox(prompt3, title3)
        If op = "+" Then
            ans = a + 
        End If


    End Sub

    Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click


    End Sub
End Class

你可以看到我没有完成它,因为我无法在第三个按钮中调用。

有什么想法吗?

如果我搞砸了,请指出。

1 个答案:

答案 0 :(得分:0)

在button1

之外定义变量
Public Class Form1
    Dim a As Integer
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 Dim prompt1, title1 As String
 prompt1 = "Enter First Number : "
 title1 = "First Number"
 a = InputBox(prompt1, title1)
End Sub

现在你可以使用变量'a'