Не доступен в этом контексте, потому что это "Частный"

Я пишу код для калькулятора и получаю эту ошибку. У меня есть математические функции в другом классе, но переменные из form1 не доступны. Ниже мой код.

Я также попытался изменить мои переменные Dim на public, но это также не работает.

Public Class Form1

'create a value to keep track of whether the calculation is complete so the calculator can revert to zero for new calculations
Dim state As Integer
'create values to store information on numbers and signs entered as well as the answer returned
Dim one As Double
Dim two As Double
Dim ans As Double
Dim sign As Char

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'when "Return" or "Enter" Key is selected button 15 is clicked (seems to only work when textbox selected????)
    Me.AcceptButton = Button15

    'change the title of the form
    Me.Text = "Simple Calculator"

    'label the buttons logically
    Button1.Text = "1"
    Button2.Text = "2"
    Button3.Text = "3"
    Button4.Text = "4"
    Button5.Text = "5"
    Button6.Text = "6"
    Button7.Text = "7"
    Button8.Text = "8"
    Button9.Text = "9"
    Button10.Text = "0"
    Button11.Text = "+"
    Button12.Text = "-"
    Button13.Text = "X"
    Button14.Text = "/"
    Button15.Text = "Calc"
    Button16.Text = "About Me"
    Button17.Text = "Clear"

    'allows form to revcieve key events
    Me.KeyPreview = True

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'create action when button 1 is clicked

    'if state is 1 then previous calculation was solved, then clear textbox to allow for new input
    If state = 1 Then
        TextBox1.Text = ""

        'insert 1 into textbox
        Dim Int1 As Integer = 1
        TextBox1.Text = TextBox1.Text & Int1

        'return state of calculator to zero to designate that calculation has NOT been solved
        state = 0

    Else
        'else insert 1 into textbox
        Dim Int1 As Integer = 1
        TextBox1.Text = TextBox1.Text & Int1

    End If

End Sub

' the above function for each numbered button

Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int2 As Integer = 2
        TextBox1.Text = TextBox1.Text & Int2

        state = 0

    Else
        Dim Int2 As Integer = 2
        TextBox1.Text = TextBox1.Text & Int2
    End If


End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    If state = 1 Then
        TextBox1.Text = ""

        Dim Int3 As Integer = 3
        TextBox1.Text = TextBox1.Text & Int3

        state = 0

    Else
        Dim Int3 As Integer = 3
        TextBox1.Text = TextBox1.Text & Int3
    End If
End Sub

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

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int4 As Integer = 4
        TextBox1.Text = TextBox1.Text & Int4

        state = 0

    Else
        Dim Int4 As Integer = 4
        TextBox1.Text = TextBox1.Text & Int4
    End If

End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int5 As Integer = 5
        TextBox1.Text = TextBox1.Text & Int5

        state = 0

    Else
        Dim Int5 As Integer = 5
        TextBox1.Text = TextBox1.Text & Int5
    End If

End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int6 As Integer = 6
        TextBox1.Text = TextBox1.Text & Int6

        state = 0

    Else
        Dim Int6 As Integer = 6
        TextBox1.Text = TextBox1.Text & Int6
    End If

End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int7 As Integer = 7
        TextBox1.Text = TextBox1.Text & Int7

        state = 0

    Else
        Dim Int7 As Integer = 7
        TextBox1.Text = TextBox1.Text & Int7
    End If

End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int8 As Integer = 8
        TextBox1.Text = TextBox1.Text & Int8

        state = 0

    Else
        Dim Int8 As Integer = 8
        TextBox1.Text = TextBox1.Text & Int8
    End If

End Sub

Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int9 As Integer = 9
        TextBox1.Text = TextBox1.Text & Int9

        state = 0

    Else
        Dim Int9 As Integer = 9
        TextBox1.Text = TextBox1.Text & Int9
    End If

End Sub

Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click

    If state = 1 Then
        TextBox1.Text = ""

        Dim Int0 As Integer = 0
        TextBox1.Text = TextBox1.Text & Int0

        state = 0

    Else
        Dim Int0 As Integer = 0
        TextBox1.Text = TextBox1.Text & Int0
    End If

End Sub

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click

    'create an action for when addition button is clicked
    Try
        'when button is clicked dim sign and text in textbox
        sign = "+"
        one = TextBox1.Text
        TextBox1.Text = ""
    Catch ex As Exception
        TextBox1.Text = "Error" ' if error occurs return error in textbox
        state = 1 'if error occurs return state to 1 to allow for new calculation
    End Try

End Sub

'repeat the above action for remainder of arithmic functions

Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
    Try
        'when button is clicked dim sign and text in textbox
        sign = "-"
        one = TextBox1.Text
        TextBox1.Text = ""
    Catch ex As Exception
        TextBox1.Text = "Error" ' if error occurs return error in textbox
        state = 1 'if error occurs return state to 1 to allow for new calculation
    End Try

End Sub


Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
    Try
        sign = "*"
        one = TextBox1.Text
        TextBox1.Text = ""
    Catch ex As Exception
        TextBox1.Text = "Error" ' if error occurs return error in textbox
        state = 1 'if error occurs return state to 1 to allow for new calculation
    End Try

End Sub

Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
    Try
        sign = "/"
        one = TextBox1.Text
        TextBox1.Text = ""
    Catch ex As Exception
        TextBox1.Text = "Error"
        state = 1 'if error occurs return state to 1 to allow for new calculation
    End Try

End Sub

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click

    'run functions based on sign stored during calculation
    Try
        two = TextBox1.Text
        If sign = "+" Then
            Calculator2.Math.add()
        ElseIf sign = "-" Then
            Calculator2.Math.minus()
        ElseIf sign = "*" Then
            Calculator2.Math.multiply()
        ElseIf sign = "/" Then
            Calculator2.Math.divide()
        End If

        'if user attempts to divide by zero return divide by zero error in textbox
        If TextBox1.Text = "Infinity" Then
            TextBox1.Text = "Divide by Zero Error"
            state = 1 'if error occurs return state to 1 to allow for new calculation
        End If
    Catch ex As Exception
        TextBox1.Text = "Error"
        state = 1 'if error occurs return state to 1 to allow for new calculation
    End Try

End Sub

Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click

    'create message box that provides information about author
    Dim msg = "Student Name: Emily Wong" & vbCrLf & "Student Number: 0692740" ' Define the message you want to see inside the message box.
    Dim title = "About Me" ' Define a title for the message box. 
    Dim style = MsgBoxStyle.OkOnly ' make an ok button for the msg box
    Dim response = MsgBox(msg, style, title)

End Sub

Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click

    'create a clear button to clear textboxes when clicked and reset state of calculator
    TextBox1.Text = ""
    state = 0

End Sub

И это мой другой класс

Public Class Math
Inherits Form1

'create funtions for add,sub, multiply, and divide features
Sub add()

    ans = one + two 'creates calculation of the entered numbers
    TextBox1.Text = ans 'returns answer into the textbox
    state = 1 'returns state to 1 to show that calculation has occured
End Sub
Sub minus()

    ans = one - two
    TextBox1.Text = ans
    state = 1
End Sub
Sub multiply()

    ans = one * two
    TextBox1.Text = ans
    state = 1
End Sub
Sub divide()

    ans = one / two
    TextBox1.Text = ans
    state = 1
End Sub

Конечный класс

2 ответа

state является частной переменной в Form1, Вы не можете получить доступ из кода за пределами Form1, Ваш Math класс не может ссылаться state, Удалить звонки state от Math код класса. Внешний класс не должен изменять состояние другого объекта в любом случае.

Попробуйте это вместо этого:

Public Class accounting
Dim Operand1 As Double
Dim Operand2 As Double
Dim [Operator] As String

Это кнопки от 1 до 0

Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button19.Click, Button12.Click, Button11.Click, Button10.Click
    txtans.Text = txtans.Text & sender.text
End Sub

Эта кнопка для периода / точки

Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
    If InStr(txtans.Text, ".") > 0 Then
        Exit Sub
    Else
        txtans.Text = txtans.Text & "."
    End If
End Sub

Эта кнопка для очистки или "C" в калькуляторе

Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
    txtans.Text = ""
End Sub

Это для сложения, вычитания, деления и умножения

Private Sub Buttonadd_Click(sender As Object, e As EventArgs) Handles Button13.Click
    Operand1 = Val(txtans.Text)
    txtans.Text = ""
    txtans.Focus()
    [Operator] = "+"
End Sub

Private Sub Buttondivide_Click(sender As Object, e As EventArgs) Handles Button15.Click
    Operand1 = Val(txtans.Text)
    txtans.Text = ""
    txtans.Focus()
    [Operator] = "-"
End Sub

Private Sub Buttonsubtract_Click(sender As Object, e As EventArgs) Handles Button16.Click
    Operand1 = Val(txtans.Text)
    txtans.Text = ""
    txtans.Focus()
    [Operator] = "*"
End Sub

Private Sub Buttonmultiply_Click(sender As Object, e As EventArgs) Handles Button14.Click
    Operand1 = Val(txtans.Text)
    txtans.Text = ""
    txtans.Focus()
    [Operator] = "/"
End Sub

Эта кнопка предназначена для знака "="

 Private Sub Buttoneequal_Click(sender As Object, e As EventArgs) Handles Button17.Click
    Dim Result As Double
    Operand2 = Val(txtans.Text)

    'If [Operator] = "+" Then
    '    Result = Operand1 + Operand2
    'ElseIf [Operator] = "-" Then
    '    Result = Operand1 - Operand2
    'ElseIf [Operator] = "/" Then
    '    Result = Operand1 / Operand2
    'ElseIf [Operator] = "*" Then
    '    Result = Operand1 * Operand2
    'End If

    Select Case [Operator]
        Case "+"
            Result = Operand1 + Operand2

            txtans.Text = Result.ToString("#,###.00")
        Case "-"
            Result = Operand1 - Operand2
            txtans.Text = Result.ToString("#,###.00")
        Case "/"
            Result = Operand1 / Operand2
            txtans.Text = Result.ToString("#,###.00")
        Case "*"
            Result = Operand1 * Operand2
            txtans.Text = Result.ToString("#,###.00")
    End Select

    txtans.Text = Result.ToString("#,###.00")
End Sub

Эта кнопка для эффекта возврата.

Private Sub Buttondel_Click(sender As Object, e As EventArgs) Handles Button21.Click
    If txtans.Text < " " Then
        txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1 + 1)
    Else
        txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1)
    End If
End Sub

Обратите внимание, что "txtans.text" - это текстовое поле, в котором пользователь вводит данные и где показывает результат.

Другие вопросы по тегам