vb.net - Assign a math operator to a variable without IF - System.MissingMemberException -
i trying send operation sign via string variable (cal_operation
) error.
notes:
- label1 hold result
- text1 hold 1 of 2 numbers make calculation on them
- text2 hold second number of 2 numbers make calculation on them
- cal_operation string hold operation sign (- or + or / or - )
the code is:
dim cal_operation string public newops = new dictionary(of string, func(of double, double, double))() { {"+", function(x, y) x + y}, {"-", function(x, y) x - y}, {"*", function(x, y) x * y}, {"/", function(x, y) x / y}
and have radio buttons assign operation string variable
in button insert code
radiobutton1_checkedchanged(sender object, e eventargs) handles radiobutton1.checkedchanged cal_operation = "+" dim op1 = newops(cal_operation) label1.text = cstr(op1(cdbl(textbox1.text), cdbl(textbox2.text))) end sub
but error
an unhandled exception of type 'system.missingmemberexception' occurred in microsoft.visualbasic.dll additional information: no default member found type 'func(of double,double,double)'.
what reason , solution?
you need change declaration of newops
dictionary , explicit on type
public newops dictionary(of string, func(of double, double, double)) = new dictionary(of string, func(of double, double, double))() { {"+", function(x, y) x + y}, {"-", function(x, y) x - y}, {"*", function(x, y) x * y}, {"/", function(x, y) x / y}}
Comments
Post a Comment