creating expressions dynamically?

Is there an equivalent function in VB to eval() in javascript
I vaugley rememebr being able to use eval() to build expressions on the fly.

example:

for i = 0 to arSeg.length -1
   for j = 1 to 4
        if instr(arSeg(i),j)<>0 then
          result = eval(seg&j) ' result should be the value of variable seg1 through seg4
        end if 
    next j
next i





Answer this question

creating expressions dynamically?

  • Raj Chidipudi

    may be this one could be what you're looking for <a href="http://msdn.microsoft.com/msdnmag/issues/02/09/CuttingEdge/default.aspx"> Using an Eval Function in Web Services</a>
  • Gisela

    Thanx Jacob,

    Found this:
    Although I'm not sure I understand it yet.


    'You can use the CallByName VB methode of the
    'Microsoft.VisualBasic.Interaction class.

    'Or you can create your own function

    Public Function MyCallByName(ByVal O As Object, ByVal functionName As
    String, ByVal Parameters() As Object) As Object
    Try
    Dim T As Type
    Dim MI As MethodInfo
    Dim ParamTypes(Parameters.Length - 1) As Type
    Dim i As Integer

    For i = 0 To Parameters.Length - 1
    ParamTypes(i) = Parameters(i).GetType
    Next i

    T = O.GetType
    MI = T.GetMethod(functionName, ParamTypes)

    Return MI.Invoke(O, Parameters)

    MI = Nothing
    T = Nothing
    Catch
    Return Nothing
    End Try
    End Function

    'The first parameter is the object or the module where the function is
    'implemented.

    'For exemple, you can use the functions like that :

    Dim S As String

    S = ""
    S = S.Concat("Essai", " Toto")
    Debug.WriteLine(S)

    S = ""
    S = CType(CallByName(S, "Concat", CallType.Method, New Object() {"Essai", "
    Toto"}), String)
    Debug.WriteLine(S)

    S = ""
    S = CType(MyCallByName(S, "Concat", New Object() {"Essai", " Toto"}),
    String)
    Debug.WriteLine(S)


    we'll see how it pans out.
    Todd

  • Grouchypb

    Unfortunately, there is no Eval equivalent in .NET.  I have a very, very vague recollection of someone putting out a library that did some basic forms of evaluation, but I can't recall where I saw it... Perhaps some aggressive searching with google or at www.dotnet247.com will help bring it to light... Sorry can't be of more help.
  • Googhum

    Yes I don't know of any Eval in .Net but I love utilizing the System.Reflection namspace (which is what some of the code above uses) to create types or call methods via a string name.  Of course the Reflection namespace is much more powerful than just that.

  • creating expressions dynamically?