CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Eval Function

  1. #1
    Join Date
    Jun 2002
    Posts
    34

    Eval Function

    we have eval function in Jscript

    is there anything we can use in vb

    The eval function allows dynamic execution of JScript source code. For example, the following code creates a new variable mydate that contains a Date object:

    eval("var mydate = new Date();");
    The code passed to the eval method is executed in the same context as the call to the eval method.


    eval function is there is vbscript
    but i don't know how to run vbscript in VB

    pls Help

    thank u

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477
    You can call the eval funtion through the scriptcontrol. You can choose the language of the scriptcontrol to be either JScript or VBScript.

    Code:
    Scriptcontrol1.Eval("var mydate = new Date();") 
    'or
    Scriptcontrol1.Eval("mydate = Now()")
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jun 2002
    Posts
    34

    :(

    i have written followiing code but the value of mysdate remains blank

    Dim mydate
    ScriptControl1.Eval ("mydate = Now()")

    MsgBox mydate

  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477
    Appearantly, you cannot use the scriptcontrol like that. First of all, to something like that, you need to use the ExecuteStatement method rather than the Eval function.
    Secondly, you can only create a bridge between the control and the program by using objects and the AddObject method of the control.

    Something like this:
    Code:
    ' class module called Class1
    Public MyDate
    Public Sub ShowDate()
        Msgbox MyDate
    End Sub
    
    ' form
    Dim MyClass As New Class1
    ScriptControl1.AddObject "MyClass", MyClass
    ScriptControl1.ExecuteStatement "MyClass.MyDate = Now()"
    Msgbox MyClass.MyDate
    
    ' or
    
    ScriptControl1.AddObject "MyClass", MyClass, True 
    ScriptControl1.ExecuteStatement "MyDate = Now()" ' not using the class now
    Msgbox MyClass.MyDate
     
    ' use of eval
    Msgbox ScriptControl.Eval("1+1") ' should give 2
    If you create public subs/functions/properties in the class, you will be able to call them on the very same way from the scriptcontrol.
    Code:
    ScriptControl1.ExecuteStatement "MyObject.ShowDate"
    ' or just
    ScriptControl1.ExecuteStatement "ShowDate"
    ' depending on how you added the object
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  5. #5
    Join Date
    Nov 2004
    Posts
    1

    Exclamation Re: Eval Function

    I dont know why you have to write so many lines of Code. For me, ScriptControl1.Eval("Date") and ScriptControl1.Eval("2 + 4") worked just fine !

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