Vb.net 这个计划有效吗
我已经开发了一个小型的计算器应用程序,它运行得很好,但是尽管我是VB.net的新手,但我知道这个程序可能没有它应有的效率。这个想法是,在文本框中输入数字并按下数学运算符后,文本框将重置并继续方程,存储过去输入的值Vb.net 这个计划有效吗,vb.net,Vb.net,我已经开发了一个小型的计算器应用程序,它运行得很好,但是尽管我是VB.net的新手,但我知道这个程序可能没有它应有的效率。这个想法是,在文本框中输入数字并按下数学运算符后,文本框将重置并继续方程,存储过去输入的值 Dim input1 As Double Dim numfunction As Double 'numerical functions (null = 0, add = 1, subtract = 2, divide = 3, multiply = 4) Private Sub
Dim input1 As Double
Dim numfunction As Double
'numerical functions (null = 0, add = 1, subtract = 2, divide = 3, multiply = 4)
Private Sub btnAdd_Click(sender As Object, e As RoutedEventArgs) Handles btnAdd.Click
If txtNum.Text = "" Then
MsgBox("Please enter a number")
Else
numfunction = 1
input1 = input1 + txtNum.Text
txtNum.Text = ""
End If
End Sub
Private Sub btnEqual_Click(sender As Object, e As RoutedEventArgs) Handles btnEqual.Click
If txtNum.Text = "" Then
MsgBox("Please enter a final number")
End If
If numfunction = 1 Then
txtNum.Text = txtNum.Text + input1
input1 = 0
End If
End Sub
你能给我指出一个正确的方向吗?我应该用什么来代替“添加”或“删除”,以使我的程序在将来更有效率?请记住,BtnAdd_Click事件只是4个事件(add、sub、divide、multiply)中的一个,因此btnEqual_Click将有几个if语句,检查用户放入了什么函数,以及txtNum中是否有任何内容
提前感谢,我不要求任何人完成我的代码,但我很想看看我有什么选择,以便我将来能制作出更高效的程序。通过使用面向对象编程的强大功能,您可以通过初步的努力简化这项任务:
Public Class Form1
' hold a reference to all operations in a list of operations
Private _operations As New List(Of Operation)
' the operation currently choosen
Private Property _currentOperation As Operation
' the 2 numbers you want to perform the operations on
Private _number1 As Double = 0
Private _number2 As Double = 0
Public Sub New()
InitializeComponent()
SetupOperations()
TextBox1.Text = 0
End Sub
Public Sub ChangeOperation(operation As Operation)
_number1 = _currentOperation.FunctionDelegate.Invoke(_number1, _number2)
TextBox1.Text = _number1
_currentOperation = operation
End Sub
Private Sub SetupOperations()
_operations.Add(New Operation(Me, btnAdd, Function(x, y)
Return x + y
End Function))
' heres the crux ... you use anonymous method to define your functions hook them to the form (Me) and the related Button
' all at once
' Similar for the other operations (subtract / multiply / divide / pow, and so on)
Dim equalsOperation As New Operation(Me, btnEqual, Function(x, y)
Return y
End Function)
_operations.Add(equalsOperation)
_currentOperation = equalsOperation
End Sub
' for this example i used only one textbox and a lable indicating wheter the number entered is a valid double
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim result As Double
If Double.TryParse(TextBox1.Text, result) Then
_number2 = result
lblValid.Text = "Valid" ' tell the user that the number entered is valid or not
Else
lblValid.Text = "Invalid"
End If
End Sub
''' <summary>
''' An Operation that hooks up a button click and can execute the operation
''' </summary>
Public Class Operation
Private _owningForm As Form1
Public Property FunctionDelegate As Func(Of Double, Double, Double) ' use a delegate to a Func that returns double with 2 double parameters
Public Sub New(owningForm As Form1, boundButton As Button, functionDelegate As Func(Of Double, Double, Double))
Me.FunctionDelegate = functionDelegate
Me._owningForm = owningForm
AddHandler boundButton.Click, AddressOf boundButton_Click ' make the operation hook up on the click event
End Sub
Private Sub boundButton_Click()
_owningForm.ChangeOperation(Me)
End Sub
End Class
End Class
公共类表单1
'保留对操作列表中所有操作的引用
私有操作作为新列表(操作)
'当前选择的操作
私有属性_currentOperation作为操作
'要对其执行操作的2个数字
专用_number1为双精度=0
专用_number2为双精度=0
公共分新()
初始化组件()
设置操作()
TextBox1.Text=0
端接头
公用子交换机操作(作为操作的操作)
_number1=\u currentOperation.FunctionDelegate.Invoke(\u number1,\u number2)
TextBox1.Text=\u number1
_当前操作=操作
端接头
专用子设置操作()
_添加(新操作(Me、btnAdd、函数(x、y)
返回x+y
终端功能)
“关键是。。。您可以使用匿名方法来定义函数,并将它们挂接到表单(Me)和相关按钮上
“一下子
'其他操作类似(减法/乘/除/功率等)
Dim相等操作作为新操作(Me、btnEqual、函数(x、y)
返回y
终端功能)
_添加操作(equalsOperation)
_当前操作=相等操作
端接头
'对于这个例子,我只使用了一个文本框和一个标签,指示输入的数字是否为有效的双精度数字
私有子TextBox1_TextChanged(发送者作为对象,e作为事件args)处理TextBox1.TextChanged
结果是双倍的
如果是Double.TryParse(TextBox1.Text,result),则
_数字2=结果
lblValid.Text=“Valid”告诉用户输入的号码是否有效
其他的
lblValid.Text=“无效”
如果结束
端接头
'''
''连接按钮点击并执行操作的操作
'''
公营课运作
私人所有形式如表1所示
公共属性FunctionDelegate As Func(Of Double,Double,Double)'将委托给返回带有2个双参数的Double的Func
Public Sub New(owningForm为Form1,boundButton为Button,functionDelegate为Func(双精度、双精度、双精度))
Me.FunctionDelegate=FunctionDelegate
我
AddHandler boundButton.Click,AddressOf boundButton\u Click'使操作连接到Click事件
端接头
私有子边界按钮\单击()
_owningForm.ChangeOperation(Me)
端接头
末级
末级
希望这不会让您太困惑,我打算向您展示一个比简单例程和大量事件处理程序更大的世界您可以通过将检查代码插入单独的子例程来简化代码
Sub CheckVals()
If txtNum.Text = "" Then
MsgBox("Please enter a final number")
End If
If numfunction = 1 Then
txtNum.Text = txtNum.Text + input1
input1 = 0
End If
End Sub
然后,您将从两个按钮单击事件中引用此子项
Private Sub btnAdd_Click(sender As Object, e As RoutedEventArgs) Handles btnAdd.Click
CheckVals()
End Sub
首先,您需要打开选项Strict。。。你犯了一个传统的错误,你需要知道如何使代码更快,但却不告诉我们需要快多少。因为你根本不知道,你实际上没有测量代码现在有多快。使用秒表类。在获得测量值后,仔细考虑用户在现在所需的一微秒或20毫秒内看到结果是否真的重要。他看不出有什么区别。换句话说,您的代码已经比需要的快了一百万倍。有点无意义,不是吗?