Question Convert string to operator

Ironwil

New member
Joined
Jun 18, 2008
Messages
2
Programming Experience
1-3
I'm doing a comparison of two integer values, but don't know the type of comparison that will be used until a certain value is passed in. After determining the type of comparison ( >, <, >=, etc.) I want to assign that to a variable and pass it to a code block that performs the check. Here's a bit of code to explain:

Select Case equality
Case "gt"
comparer = ">"
Case "lt"
comparer = "<"
Case "eq"
comparer = "="
Case "ge"
comparer = ">="
Case "le"
comparer = "<="
End Select

Then I want to be able to use the comparer variable in a test, like:

If variable1 comparer variable2 Then
End If

Of course, this doesn't work, since comparer is not an operator. Is there a way to cast it into an operator, or to get it to evaluate to an operator, or is it necessary to create different tests with nearly identical code for each comparison type?
 
you could do it this way:
VB.NET:
Select Case equality
Case "gt"
  If variable1 > variable2 Then
  End If
Case "lt"
  If variable1 < variable2 Then
  End If
Case "eq"
  If variable1 = variable2 Then
  End If
Case "ge"
  If variable1 >= variable2 Then
  End If
Case "le"
  If variable1 <= variable2 Then
  End If

Case Else 'Not equal to
  If variable1 <> variable2 Then
  End If
End Select
 
Thanks, but what I'm trying to do is only write the if statement once, and pass in the operator to use at runtime. I got the project running using several if statements, but want to cut that down to one.

Anyway, I want to test for what kind of operator to use in the select statement, and pass the result of that into the if statement at runtime and have it resolve properly to an operator type. I've heard this is possible, but haven't found an answer yet.
 
Back
Top