.net - in NCalc why does 'if (12 > 8)' resolve to false? -


brand new ncalc.

i have formula evaluated , im getting unexpected (to me) results.

the @t_no_starters parameter used = 12.

if formula if (@t_no_starters >= 8 ,'t','f') result false

if formula if (@t_no_starters >= 8.0 ,'t','f') result true

if formula if (@t_no_starters >= 10 ,'t','f') result true

im not sure going on, why 8 false yet 8.0 true 10 again whole number true? how can ensure evaluation correct? turn every number decimal? can set parameter type? when im entering parameters?

below test code use arrive @ above.

private sub button40_click(sender object, e eventargs) handles button40.click         dim formulaline string = "if (@t_no_starters >= 8 ,'t','f')"          dim parms new dictionary(of string, string)         parms.add("t_no_starters", 12)         dim ouranswer = formulacalculator.msgboxeval(formulaline, parms)     end sub  function msgboxeval(formula string, parms dictionary(of string, string))          try             dim x new expression(formula)             each key string in parms.keys                 x.parameters.add(key, parms.item(key))             next              dim result = x.evaluate             messagebox.show(result)             return result         catch ex exception             messagebox.show(ex.message)             return "error " & ex.message         end try          return 0     end function 

i best not change values in formula decimal. greater or equal 8 starters not 8.0. doesen't right. parameter dictionary of string , string, have use strings. simple making sure parameter value when value required?

it's pretty late answer post future readers.

your dictionary made of strings:

dim parms new dictionary(of string, string) 

it doesn't matter original parameter type, vb convert string you:

parms.add("t_no_starters", 12) 

it's equivalent to:

parms.add("t_no_starters", "12") 

now it's obvious you're adding parameters strings:

dim x new expression(formula) each key string in parms.keys     x.parameters.add(key, parms.item(key)) next 

ncalc try convert types , evaluate expression without errors:

if (@t_no_starters >= 8 ,'t','f') 

it's equivalent to:

if (@t_no_starters >= '8' ,'t','f') 

because it's string comparison (without natural ordering) '12' >= '8' correctly false (and other examples become reasonable too).

what should do? change type of dictionary avoid implicit conversion, let ncalc you:

dim parms new dictionary(of string, object) 

and:

function msgboxeval(formula string, parms dictionary(of string, object)) 

note using object still able add type dictionary:

params.add("t_text", "some text")  params.add("t_beginning", datetime.now) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -