0

I'm new to vb.net need help to resolve this:

Following is coming from database

Param1: 100
Param2: 184
Comparion Operator: >

In vb.net - i need to generate code like this:

If 100>184 Then
'Do something
Else
'Do something
End

I'm really struggling how to generate the above code in vb.net as values are coming from database.

I haven't tried anything, im struggling to write code for this.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
Conrad Jagger
  • 643
  • 6
  • 19
  • 31
  • Are you looking for a code-generation mechanism? or you are looking for a way to generate the code and run it on-the-fly? and What have you done so far? – Alireza Maddah Jun 28 '13 at 09:04

3 Answers3

0

You have to rely on a set of conditions or a switch...case. You can get some inspiration here.

Thus, the solution for the specific case you are proposing should be something on the lines of:

Private Function checkSituation(ByVal val1 As Integer, ByVal inputOperator As String, ByVal val2 As Integer) As Boolean

    Select Case inputOperator
        Case ">"
            Return val1 > val2
        Case "<"
            Return val1 < val2
        Case "="
            Return val1 = val2
    End Select

End Function

In your case:

    If (checkSituation(100, ">", 184)) Then


    End If
Community
  • 1
  • 1
varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • I would add >=, <= and <> too. – David - Jun 28 '13 at 09:14
  • 1
    This was just a sample code to provide some help. He should take it and create a code matching his exact expectations. He might want more things. But thanks for the remark. – varocarbas Jun 28 '13 at 09:16
0

I'd implement a class Comparator that has a method which returns true if the condition is met and false if not. I'm not fluent in VB.NET, but I'd try the following in C#:

public static class Comparator
{
    public static bool Compare(int value1, int value2, string operator)
    {
        switch (operator)
        {
           case "<" : return value1 < value2;
           case "<=" : return value1 <= value2;
           case ">" : return value1 > value2;
           ...
        }
    }
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

Try something like this:

Private Function EvalData(param1 As Integer, param2 As Integer, operator As String) As Boolean
    Select Case operator
        Case ">" : Return param1 > param2
        Case "<" : Return param1 < param2
        Case "=" : Return param1 = param2
        Case Else 
            Throw New ApplicationException("No matches for this operator")
    End Select
End Function

And then use it in this way:

If EvalData(param1, param2, operator) Then
    'Do something
Else
    'Do something
End 
SysDragon
  • 9,692
  • 15
  • 60
  • 89