Page 1 of 1

Use conditional if with condition in string

Posted: Tue Mar 26, 2019 5:13 am
by aengelriv
greetings

I need to use 'if' conditions built by code
the condition in a string text
see example:

Code: Select all

Sub is_false_or_true_with_string_condition
'1) ''  with string
one="red" : two="red"   
    linecommand0=chr(34) & one & chr(34) & ""="" & chr(34) & two & chr(34)
    if linecommand0 then
   		 msgbox "Yes equal " & one
   	else
   	    msgbox "not equal"	 
    endif
  '      ------------- work ok  
'2) ''  with values
    one=6 : two=8 
    linecommand0=chr(34) & one & chr(34) & "">"" & chr(34) & two & chr(34)
    if linecommand0 then
   		 msgbox "is mayor " & one
   	else
   	    msgbox "is minor"	 
    endif
    '-------  work well

'3)   but if the operator is in variable ....  errorr... 
    part1=2 : part2=2 : operat="="
    comandline1= chr(34) & part1 & chr(34) &  chr(34) &  "" & operat &  "" & chr(34) &  chr(34) & part2 & chr(34)
     '''   here problem with variable operat << ---
    msgbox comandline1    
    if comandline1 then
      msgbox "yes"
    endif
 ' not work....  

end sub 
    
I need to place the operator in the variable because it changes according to the procedure.

How would the arrangement in the code.
thanks!

Re: Use condicional if in string

Posted: Tue Mar 26, 2019 5:41 am
by FJCC
Something like this?

Code: Select all

part1=2 : part2=2 : operat= "="

SELECT CASE operat
  Case "="
    comandline1 = (part1 = part2)
  Case ">"
    comandline1 = (part1 > part2)
  Case "<"
    comandline1 = (part1 < part2)
End Select
     
    if comandline1 then
      msgbox part1 & operat & part2
    else
      msgbox part1 & " not " & operat & part2
    endif

Re: Use conditional if in string

Posted: Tue Mar 26, 2019 9:45 am
by JeJe
my oops.

Re: Use conditional if in string

Posted: Tue Mar 26, 2019 10:16 am
by JeJe
Perhaps you could use an Eval function like this one? cbool(eval("2>1")), cbool(eval("2=2")) etc seems to work

http://ftp.planet-source-code.com/vb/sc ... 6&lngWId=1

Re: Use conditional if in string

Posted: Tue Mar 26, 2019 12:45 pm
by Lupp
There aren't just three ordinary comparators, but 6. And sometimes you may want to parametrize different kinds of calculations.

The very different approach demonstrated in the attached document is supposed to look funny. You may want to see it nonetheless.
(You can create a FunctionAccess service independent of the type of the document for which the macro is called.)

Limitation: You can not call a user defined function via the FunctionAccess service.

By the way: The subject of this thread seems somehow misleading to me.

Re: Use conditional if with condition in string

Posted: Wed Mar 27, 2019 7:44 pm
by MrProgrammer
aengelriv wrote:I need to use 'if' conditions built by code the condition in a string text … I need to place the operator in the variable because it changes according to the procedure.
Basic does not support that feature. The solution is to write your macro in Python and use eval().
aengelriv wrote:
chr(34) & one & chr(34) &           "" =          ""           & chr(34) & two & chr(34)
chr(34) & one & chr(34) &           "" >          ""           & chr(34) & two & chr(34)
chr(34) & one & chr(34) & chr(34) & "" & operat & "" & chr(34) & chr(34) & two & chr(34)
Your example is bogus because the third expression does not have the same form as the previous two. The first two have a comparison operator (= or >) between the empty strings ("") and thus they evaluate to TRUE or FALSE. The third has string concatenation between the empty strings and the expression evaluates to a string. However, any attempt like this is pointless because Basic does not support the feature you want.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.

Re: Use conditional if with condition in string

Posted: Wed Mar 27, 2019 10:35 pm
by JeJe
MrProgrammer - did you look at the link I posted... there is very often an off-the-shelf VB6 solution to these sorts of problems that will run with no/a little adaptation to OO. Finding someone else's code that does what you want is way easier...

Re: Use conditional if with condition in string

Posted: Mon Apr 01, 2019 5:36 pm
by MrProgrammer
JeJe wrote:did you look at the link I posted... there is very often an off-the-shelf VB6 solution …
However OpenOffice uses StarBasic, not VB6. I don't know what all the differences between the two might be, and I doubt the OP does either. Some tests show:
• Eval("2 * 3 ^ 2") is 36, but in StarBasic 2 * 3 ^ 2 is 18.
• Eval("(-1) Xor (0)") is 0 but in StarBasic (-1) Xor (0) is -1.
• With x=1, Eval("7+x") is 7 but in Starbasic 7+x is 8.

Rather than deal with these difficulties it seems better to use Python where an eval function is provided by the language.

Re: Use conditional if with condition in string

Posted: Mon Apr 01, 2019 7:17 pm
by JeJe
MrProgrammer - I get identical results, both in OO and in VB6, giving

MsgBox Eval("2 * 3 ^ 2") ' 36
MsgBox Eval("(-1) Xor (0)") ' 0
MsgBox Eval("7+1") '8

MsgBox 2 * 3 ^ 2 ' 18
MsgBox (-1) Xor (0) '-1
x = 1
MsgBox 7 + x '8

The only difference is in OO I got an unexpected symbol error with (-1) and the brackets had to be removed.

Your examples are interesting though - that may not be the best eval function out there, there are several to choose from.
It works within the range required by the OP though.

There are strong advocates for Python here. If you already know it - then sure. If you don't then you may easily find a VB6 function that does what you want. And Basic has the IDE and some (if limited) crossover with VBA.

Edit:
Looking again at the function.

x=1, Eval("7+x") is never going to work in any function, python or basic. X is always going to equal 0 in the string.

MsgBox Eval("-1 xor 0") gives -1 the same as -1 xor 0
You've introduced brackets and that errors in OO anyway.

The difference with ^ is explained in the comments above the function
"exponentiation ^ has same level of multiplication and division"

That's the only real quibble here - it doesn't evaluate the power first... but that limitation is explained. With that limitation its a good function I think...