实现自定义类

时间:2013-12-26 14:12:28

标签: vb.net

我已经有了这个代码并且它有效,但我对如何实现自定义类感到困惑。运营商需要改变,我已经把我学到的所有内容和迄今为止得到帮助的东西放在一起,并将其作为班级。我正在尝试将运算符更改为自定义类,因此我会使用别的东西代替+, - ,*,/。基本上这是类,在完整代码

之下
Public Class MathOp
    Public Function Add(NumOne As Integer, NumTwo As Integer) As Integer
        Return NumOne + NumTwo
    End Function

    Public Function Subtract(NumOne As Integer, NumTwo As Integer) As Integer
        Return NumOne - NumTwo
    End Function

End Class


Public Class MathOp2
Inherits MathOp

Public Function Mul(value1 As Integer, value2 As Integer) As Integer
    Return value1 * value2
End Function

Public Function Div(value1 As Integer, value2 As Integer) As Integer
    Return value1 / value2
End Function

End Class

我已将这段代码放在一起,只要使用标准运算符,该代码就可以运行。我只是不知道接下来该做什么。

Option Explicit On
Option Strict On
Option Infer Off



Public Class Form1



Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles m3.Click
    'Adds the numbers together and the tag remembers the operator

   Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles m3.Click
    'Adds the numbers together and the tag remembers the operator

    Dim m As New MathOp()
    Dim R As Integer = m.Add(CInt(NumOne.Text), CInt(NumTwo.Text))


    If NumOne.Text = ("") Then
        MessageBox.Show("Please enter the first number")
        NumOne.Focus()
    ElseIf NumTwo.Text = ("") Then
        MessageBox.Show("Please enter the second number")
        NumTwo.Focus()
    End If

    Result.Text = CStr(m.Add(CInt(NumOne.Text), CInt(NumTwo.Text)))

    ListBox1.Tag = "+"



End Sub

Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
    'Subtracts the numbers

    If NumOne.Text = ("") Then
        MessageBox.Show("Please enter the first number")
        NumOne.Focus()
    ElseIf NumTwo.Text = ("") Then
        MessageBox.Show("Please enter the second number")
        NumTwo.Focus()
    End If
    Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
    ListBox1.Tag = "-"
End Sub

Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
    'Multiplies the numbers

    If NumOne.Text = ("") Then
        MessageBox.Show("Please enter the first number")
        NumOne.Focus()
    ElseIf NumTwo.Text = ("") Then
        MessageBox.Show("Please enter the second number")
        NumTwo.Focus()
    End If

    Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
    ListBox1.Tag = "x"
End Sub

Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
    'Divides the numbers, with a divide by 0 exception

    If NumOne.Text = ("") Then
        MessageBox.Show("Please enter the first number")
        NumOne.Focus()
    ElseIf NumTwo.Text = ("") Then
        MessageBox.Show("Please enter the second number")
        NumTwo.Focus()
    End If




    Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
    ListBox1.Tag = "/"

    'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN

    Try

        If CDbl(NumTwo.Text) = Val(0) Then
            Result.Text = ""
            MessageBox.Show("You cannot divide by 0, please input another number")
            NumTwo.Focus()
        End If

    Catch ex As Exception
        Result.Text = ""

    End Try

End Sub

Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
    'Simply closes the form
    Me.Close()
End Sub

Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click

    If NumOne.Text = ("") Then
        MessageBox.Show("Please enter some numbers")
        NumOne.Focus()
    ElseIf NumTwo.Text = ("") Then
        MessageBox.Show("Please enter another number")
        NumTwo.Focus()
    End If

    Try
        If ListBox1.Items.Count < 10 Then
            ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & ("=") & Result.Text)
        End If
    Catch ex As Exception
        Result.Text = ""

    End Try
End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonClear.Click
    ListBox1.Items.Clear()
End Sub


Private Sub ButtonReset_Click_1(sender As Object, e As EventArgs) Handles ButtonReset.Click
    NumOne.Text = ""
    NumTwo.Text = ""
    Result.Text = ""
End Sub



Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim toolTip1, toolTip2, toolTip3, toolTip4, toolTip5, toolTip6, toolTip7, toolTip8 As New ToolTip()

    toolTip1.SetToolTip(Me.m3, "Addition")
    toolTip2.SetToolTip(Me.ButtonSub, "Subtraction")
    toolTip3.SetToolTip(Me.ButtonMul, "Multiplication")
    toolTip4.SetToolTip(Me.ButtonDiv, "Division")
    toolTip5.SetToolTip(Me.ButtonSave, "Save operation")
    toolTip6.SetToolTip(Me.ButtonReset, "Reset operation")
    toolTip7.SetToolTip(Me.ButtonExit, "Exit application")
    toolTip8.SetToolTip(Me.ButtonClear, "Clear list")




End Sub

Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick


    'This is part of the second assignment, double click on list items and they show up in the text boxes, still working on the other part

    If ListBox1.SelectedIndex = -1 Then
        Return
    End If

    'Get the text of the selected item
    Dim selectedtext As String = ListBox1.Items(ListBox1.SelectedIndex).ToString()

    'Split the item by the operator and the = into an array of strings
    Dim parts As String() = selectedtext.Split("+"c, "-"c, "x"c, "/"c, "="c)

    If parts.Length > 2 Then
        'Define a variable for each part
        Dim part1 As String = parts(0).Trim()
        Dim part2 As String = parts(1).Trim()

        NumOne.Text = part1
        NumTwo.Text = part2

        'Make text boxes set to part1 and part2. part1 = 1, part2 = 1
    End If

End Sub
End Class

1 个答案:

答案 0 :(得分:2)

VB.NET允许您重载某些运算符,但它不允许您创建新的自定义运算符。有关VB.NET中运算符重载的更多信息,以及获取支持重载的运算符列表,请参阅MSDN上的this page

从你的问题中不清楚你究竟想要完成什么。但是,根据您的示例,似乎运算符重载可能不是您正在尝试执行的操作的正确解决方案。我认为您可能想要做的是拥有多个MathOp类,每个运算符一个,并让它们都实现相同的接口,该接口具有Evaluate方法,该方法对两个操作数执行操作,返回结果。实际的操作符(例如“+”)只是一个字符串属性。

修改

以下是我认为你真正需要做的一个例子:

Public Class MathOp
    Public Function Add(value1 As Integer, value2 As Integer) As Integer
        Return value1 + value2
    End Function

    Public Function Subtract(value1 As Integer, value2 As Integer) As Integer
        Return value1 - value2
    End Function
End Class

然后,要使用它,你需要做这样的事情:

Dim m As New MathOp()
Dim result As Integer = m.Add(2, 2)  ' Returns 4