Android Question Convert String to Operator

JohnD

Active Member
Licensed User
Longtime User
I am trying to convert the string "*" to the multiplication operator for an on the fly creation of a calculation. Below are the the stabs in the dark I have tried and their respective error messages reported by the compiler.

In all cases assume: Dim sMult = "*" as String

Code: Copy
'1
iAns = (iNum1) &sMult (iNum2)
'2
iAns = iNum1 &sMult iNum2
'3
iAns = iNum1 "\sMult" iNum2

Error 1
Parsing code. Error
Error parsing program.
Error description: Use of undeclared array: smult
Occurred on line: 34
iAns = (iNum1) &sMult (iNum2)

Error 2
Parsing code. Error
Error parsing program.
Error description: Syntax error.
Occurred on line: 34
iAns = iNum1 &sMult iNum2

Error 3
Parsing code. Error
Error parsing program.
Error description: Syntax error.
Occurred on line: 35
iAns = iNum1 "\sMult" iNum2

Any help on this would be greatly appreciated. Thanks, JD
 
You can't convert a string or character to an operator.
You would need something like this:
Code: Copy
Select sMult
Case "+"
    iAns = iNum1 + iNum2
Case "-"
    iAns = iNum1 - iNum2
Case "*"
    iAns = iNum1 * iNum2
Case "/"
    If Abs(iNum) > 1E-15 Then
        iAns = iNum1 / iNum2
    Else
        ToastMessage("Error: Zero devider", False)
    End If
End Select
 
Upvote 0
Not clear whar you are doing here, is iAns a string? Num1 & 2 I assume are values not string.
Do you want to build a string like: "4 × 5 = 20"? Pls explain a little better.
 
Upvote 0
Ok Klaus was quicker!
 
Upvote 0
@klaus - What I expected and what I'm currently doing. But I figured it wouldn't hurt to ask. My ultimate implementation is to store user-defined calculations in a db. My plan is to store variables in one db field and operators in another - possibly comma-demimited in their respective fields for readability. Populate arrays from each field and pass the incremental calculations through case statements. It would have been nice to eliminate the case statements though.

Oh, thanks for the reply and the phantom-decimal digits logic for division. :)
 
Upvote 0
Back
Top