VB 6.0 - string as condition in if statement? - Asked By Prabhakaran on 10-Nov-11 04:49 AM

hi friends, a = 5
b = 10
stmt = "A > B"

If (stmt) Then
MsgBox "valid"
else
mstbox "invalid"
End If

how to do in vb6.0 
condition use variable 'stmt' only
it produce error? any solution for this?
Chintan Vaghela replied to Prabhakaran on 10-Nov-11 04:52 AM
Hello

Try this way

If a > b Then

MsgBox "valid"
else
mstbox "invalid"
End If


Hope this helpful
Prabhakaran replied to Chintan Vaghela on 10-Nov-11 05:11 AM
No i must use stmt only
Jitendra Faye replied to Prabhakaran on 10-Nov-11 05:17 AM
You can not match condition like this, because if statement accept condition, and condition is used between operands.

Kirtan Patel replied to Prabhakaran on 10-Nov-11 05:18 AM
you need to create Function that Evaluate Expression and produce result according to stirng expression like below is example of it 

Function EvalExpression(ByVal expression As String) As Double
    Dim result As Double
    Dim operand As Double
    Dim opcode As String
    Dim index As Integer
    Dim lastIndex As Integer
    
    ' the null character mark the end of the string
    expression = expression & vbNullChar
    
    For index = 1 To Len(expression) + 1
        If InStr("+-*/" & vbNullChar, Mid$(expression, index, 1)) Then
            If lastIndex = 0 Then
                ' this is the first operand in the expression
                result = Val(Left$(expression, index - 1))
            Else
                ' extract the new operand
                operand = Val(Mid$(expression, lastIndex, index - lastIndex))
                ' execute the pending operation
                Select Case opcode
                Case "+"
                    result = result + operand
                Case "-"
                    result = result - operand
                Case "*"
                    result = result * operand
                Case "/"
                    result = result / operand
                End Select
            End If
            opcode = Mid$(expression, index, 1)
            lastIndex = index + 1
        End If
    Next
    EvalExpression = LTrim$(result)
    
End Function