vb.net - Is this program efficient -
i've put small calculator application works quite well, despite being novice vb.net know program isn't efficient should be. idea upon inputting numbers textbox , pressing mathematical operator, textbox reset , continue equation, storing past values entered.
dim input1 double dim numfunction double 'numerical functions (null = 0, add = 1, subtract = 2, divide = 3, multiply = 4) private sub btnadd_click(sender object, e routedeventargs) handles btnadd.click if txtnum.text = "" msgbox("please enter number") else numfunction = 1 input1 = input1 + txtnum.text txtnum.text = "" end if end sub private sub btnequal_click(sender object, e routedeventargs) handles btnequal.click if txtnum.text = "" msgbox("please enter final number") end if if numfunction = 1 txtnum.text = txtnum.text + input1 input1 = 0 end if end sub
could point me in right direction should replace add or remove make programs more efficient in future? keep in mind btnadd_click event 1 of 4 (add, sub, divide, multiply) , because of btnequal_click have few if statements, checking function user has put in, , if there in txtnum @ all.
thanks in advance, i'm not asking complete code, i'd love see options have make more efficient programs in future.
with little initial effort simplify task using power of object oriented programming:
public class form1 ' hold reference operations in list of operations private _operations new list(of operation) ' operation choosen private property _currentoperation operation ' 2 numbers want perform operations on private _number1 double = 0 private _number2 double = 0 public sub new() initializecomponent() setupoperations() textbox1.text = 0 end sub public sub changeoperation(operation 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 crux ... use anonymous method define functions hook them form (me) , related button ' @ once ' similar other operations (subtract / multiply / divide / pow, , on) dim equalsoperation new operation(me, btnequal, function(x, y) return y end function) _operations.add(equalsoperation) _currentoperation = equalsoperation end sub ' example used 1 textbox , lable indicating wheter number entered valid double private sub textbox1_textchanged(sender object, e eventargs) handles textbox1.textchanged dim result double if double.tryparse(textbox1.text, result) _number2 = result lblvalid.text = "valid" ' tell user number entered valid or not else lblvalid.text = "invalid" end if end sub ''' <summary> ''' operation hooks button click , can execute operation ''' </summary> public class operation private _owningform form1 public property functiondelegate func(of double, double, double) ' use delegate func returns double 2 double parameters public sub new(owningform form1, boundbutton button, functiondelegate func(of double, double, double)) me.functiondelegate = functiondelegate me._owningform = owningform addhandler boundbutton.click, addressof boundbutton_click ' make operation hook on click event end sub private sub boundbutton_click() _owningform.changeoperation(me) end sub end class end class
hope not confusing you, intended show bigger world simple routines , tons of eventhandlers
Comments
Post a Comment