Calc problem... already :-(

Discussion in 'Questions (Windows Mobile)' started by RandomCoder, Oct 9, 2007.

  1. RandomCoder

    RandomCoder Well-Known Member Licensed User

    I'm attempting a go at the calculator competition (see this thread http://www.basic4ppc.com/forum/showthread.php?t=914).

    In doing so I would like to take what I believe is a rather unique approach, however I am already experiencing problems and would like your help.

    The problem is this....

    Code:
    x = 2 + 3
    MsgBox(x)
    Works like a charm, giving 5 as the result (can't really make it more simple than that)

    But now when I do this instead...
    Code:
    x = 2 & chr(43) & 3
    MsgBox(x)
    I get 2+3 instead of the desired mathematical result.

    So is there a way of forcing the compiler to treat the expression as an equation rather than a string?

    Regards,
    RandomCoder
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. agraham

    agraham Expert Licensed User

    I personally don't like the weak typing of B4PPC but you can get it to work for you.

    Code:
    x = 2 & Chr(43) & 3 & "*" & 4 ' build a valid expression 
    y = 0 + x ' coercion forces evaluation of the string
    Msgbox(y) ' success
    If you use this then you will need an error handler unless you can parse the string for mathematical correctness first.

    EDIT: Or use Isnumber() on the string and not try the coercion if it returns false. This approach supports */+- and ^ but not functions such as abs() etc.

    EDIT EDIT: Actually Isnumber() isn't reliable in this instance. I don't know why I thought it would be. Stick to the error handler approach!
     
    Last edited: Oct 9, 2007
  4. Erel

    Erel Administrator Staff Member Licensed User

    This trick is dangerous.
    I recommend you to use the code sample in the link I've posted instead.
    It will only work if evaluated once.
    This code for example will fail on the second time:
    Code:
    Sub App_Start
        
    For i = 1 To 2
            x = 
    "2*5+4" 
            y = 
    0 + x 
            
    Msgbox(y)
        
    Next
    End Sub
    The reason it fails is that there is an optimization process that optimizes calculations after the first calculation.
     
  5. agraham

    agraham Expert Licensed User

    Pity - I thought that it might be quite useful:sign0148:


    EDIT: Actually this probably explains some inconsistent results and inexplicable errors I have encountered trying some fairly complicated string maniipulation where I was (rather less blatently than above) using B4PPCs apparent use of weak typing to reduce the amount of code.
     
    Last edited: Oct 9, 2007
  6. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Thanks for the input guys, I had already tried using a variety of amplisand, brackets and single quotes but all to no avail.

    With some help from a friend of mine he has got it working using the following method...

    Code:
    x=(2 Chr(433)
    MsgBox(x)
    I must admit that I had already tried this without the brackets and all I got was a value of 2, but with the brackets it's evaluated correctly and produces a value of 5.

    Thanks for the help.

    Regards,
    RandomCoder
     
  7. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice