CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 1999
    Posts
    1

    Sub/Function stored in Variable possible?

    Hi,

    Is it possible (and how) to store a function call in a variable, then "evaluate" the variable? In other words, can I do the following:

    function test (x,y)
    test = x + y
    end function

    myVar = "test(1,2)"
    myResult = evaluate(myVar)

    Now, myResult should be 3.

    If this is not possible in VB, is there a way around this? Some tricks, maybe?

    Please help.
    Thanks



  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Sub/Function stored in Variable possible?

    you can use the Microsoft Script control to do that. This OCX has an Eval method.


  3. #3
    Join Date
    Jul 1999
    Location
    Israel
    Posts
    140

    Re: Sub/Function stored in Variable possible?

    I have try to put this control in my form and perform ScriptControl1.Eval("test(1,2)") and it doesn't work. What i did wrong?


  4. #4
    Join Date
    Aug 1999
    Posts
    10

    Re: Sub/Function stored in Variable possible?

    If you have VB6 then you can use CallByName function. But you would have to have your functions in a Class Module (not regular mudule) because CallByName function has first parameter as an object.

    Syntax: CallByName(object, procname, calltype,[args()])

    Example:
    iSum = CallByName(Class1, "Multiply", VbMethod, 5, 5)
    MsgBox iSum


    Assuming that I have function Multiply in calss called Class1.

    Regards,


    Serge
    Software Developer
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured