Resumen Evaluación de Expresión (EVAL)

Mensaje enviado por "Santiago Fumanal" <sfpaul@mail.fujitsu.es>

Te mando un ejemplo espero que te sirva.


Add Code to a Procedure

Before you can run the NameMe procedure, use the AddCode method to add the complete procedure to the Script Control. If you try to add an incomplete procedure (one with no End Sub or End Function), an error will occur. The following example adds procedure code to the Script Control:

' When the ScriptRun app loads, the following code
' adds the NameMe procedure to the Control.
Private Sub Form_Load()
 Dim strCode As String
 strCode = _
   "Sub NameMe()" & vbCrLf & _
 " Dim strName As String" & vbCrLf & _
 " strName = InputBox(""Name?"")" & vbCrLf & _
   " MsgBox ""Your name is "" & strName" & vbcrLf & _
 "End Sub"
 ScriptControl1.AddCode strCode
End Sub

Alternatively, you can add procedure code using a TextBox control:

Private Sub Form_Load()
 ' The code is contained in the Textbox named
 ' txtScript on the form named frmScript.
 ScriptControl1.AddCode frmScript.txtScript.Text
End Sub

You can add arguments to a procedure or function.

Private Sub EvalFunc()
 ' Create the function.
 Dim strFunction As String
 strFunction = _
 "Function ReturnThis(x, y)" & vbCrLf & _
 " ReturnThis = x * y" & vbCrLf & _
 "End Function"
 ' Add the code, then run the function.
 ScriptControl1.AddCode strFunction
 MsgBox ScriptControl1.Run("ReturnThis", 3, 25)
End Sub

Once code has been added using the AddCode method, you can then use the Run method to run the procedure.

Run the Procedure

The Run method runs any complete procedure that has been added to the Script Control. The following code fragment runs three defined procedures:

ScriptControl1.Run "FindName"
ScriptControl1.Run "AddName"
ScriptControl1.Run "Quit"

Executing Scripting Statements

To execute scripting code, the Script Control features the ExecuteStatement method. For example, the following code executes a MsgBox statement:

Private Sub Command1_Click()
 ' Create a message box with the word Hello in it.
 ScriptControl1.ExecuteStatement "MsgBox ""Hello"""
End Sub

Evaluating Scripting Statements

You can also evaluate lines of scripting code using the Eval method. The Eval method simply tests a line of scripting code, as shown in the following example:

Private Sub TryThis()
    ScriptControl1.ExecuteStatement "x = 100"
    MsgBox ScriptControl1.Eval("x = 100") ' True
    MsgBox ScriptControl1.Eval("x = 100/2") ' False
End Sub

In the preceding code, the ExecuteStatement method executes the statement and assigns the value 100 to the variable x. The next two lines use the Eval method to test the statements x = 100 and x = 100/2. The second line returns True, and the third returns False.

Creating an Instance of the Script Control

The Microsoft Script Control can be created as either a control or a standalone Automation object. This feature allows the Script Control to be used by any host, using any scripting language. The following example can be placed in any form. Note that the variable sc is not declared as type ScriptControl because the Control is not and doesn't need to be referenced in the project. As long as the Script Control is present and registered, the following code will work:

Private Sub Command1_Click()
 Dim sc ' This can't be early-bound!
 Dim strProgram As String
 strProgram = "Sub Main" & vbCrLf & _
 "MsgBox ""Hello World""" & vbCrLf & _
 "End Sub"
 Set sc = CreateObject("ScriptControl")
 sc.language = "VBScript"
 sc.addcode strProgram
 sc.run "Main"
End Sub

Using the AllowUI Property

The AllowUI property determines if the scripting engine is permitted to display user interface elements. This can apply to the Script Control itself, such as when it displays timeout messages. It can also apply to scripting engines that use ActiveX scripting interfaces. For example, the following code will generate an error:

ScriptControl1.AllowUI = False
Dim strX As String
strX = "Sub Hello" & vbCrLf & _
"MsgBox ""Hello, World""" & vbCrLf & _
"End Sub"
ScriptControl1.AddCode strX
ScriptControl1.Run "Hello" ' No UI allowed!

Using the Error Property

With the Script Control, errors may come from two sources: the Script Control itself, or the script the Control is attempting to run. In the latter case, to debug scripting code, the Script Control features the Error property, which returns a reference to the Error object. Using the Error object, the Script Control can return the number and description of the error, and also the the line number where the error occurs in the script.
Run the following code to see an example of the Control finding an error:

Private Sub MyError()
 ' The scripting code below divides by zero raising
 ' an error.
 Dim strCode As String
 strCode = _
 "Sub DivideByZero()" & vbCrLf & _
 "Dim prime" & vbCrLf & _
 "prime = 3" & vbCrLf & _
 "MsgBox prime/0" & vbCrLf & _
 "End Sub"
 On Error GoTo scError
 With ScriptControl1
  .AddCode strCode
  .Run "DivideByZero"
 End With
 Exit Sub
scError:
 ' Use the Error object to inform the user of the
 ' error, and what line it occured in.

Debug.Print ScriptControl1.Error.Number & _
 ":" & ScriptControl1.Error.Description & _
 " in line " & ScriptControl1.Error.Line
Exit Sub
End Sub
-----Original Message-----
From: Iñigo Bartolome <ibartolome@ikt.es>
To: visualbasic-esp@egroups.com <visualbasic-esp@egroups.com>
Date: Friday, May 12, 2000 9:52 AM
Subject: (VB-ESP) Evaluación de Expresión (EVAL)

            Hola Listeros. Me gustaria saber si existe para Visual Basic una función que tenga la misma funcionalidad que el EVAL de
Access. Existe el Script Control v. 1.0 de Microsoft que expone la función EVAL, pero no me evalua funciones creadas por mi al igual que hace el EVAL de Access. Gracias

        Ejemplo:

Private Function Suma(x as integer, y as integer) as Integer
            suma = x+y
End function

Private Sub Evaluar
         'En Access
         Eval(Suma(1,1)=2) 'Devuelve -1

         'Con la función del OCX
         Eval(Suma(1,1)=2) 'Error No coinciden los tipos.
End sub

                        Un Saludo



Resumen Resumen

Visual Basic Página de Visual Basic

Página principal Página principal

www.jrubi.com