eval的问题
引用了Script Control 1.0 这个组件Public Function Eval(ByRef strEval)
'On Error Resume Next
Dim EvalObject As New ScriptControl
EvalObject.Language = "VBScript"
EvalObject.AddObject "Me", ESE_Object, True
Eval = EvalObject.Eval(strEval)
Set EvalObject = Nothing
End Function
这个只能执行 像
a = "b"
b = "c"
Eval("a+b")
但是像 instr 等就无法执行
a = "a,b,c,d"
b = "b"
c = "instr(a,b)"
eval(c) --------------------编程问答-------------------- --------------------编程问答--------------------
Dim e As New ScriptControl--------------------编程问答-------------------- 没准楼主写的就是2楼说的代码,但这又和ScriptControl扯得上啥关系呢:) --------------------编程问答--------------------
e.Language = "VBScript"
a = 1
b = 2
Debug.Print e.Eval(a + b), e.Eval(InStr(a, b))
Set e = Nothing
楼上果然可以,但是现在有个问题,不知好不好解决
InStr 我也是做变量传的
c = "InStr(a,b)"
如果是这样,应该做才可以呢? --------------------编程问答--------------------
楼主没完全明白ScriptControl的作用。呵呵。
举个例子:
建个类,名为"toury";里面就一个公有属性声明Public test As String
然后在FORM里
Private Sub Form_Load()--------------------编程问答--------------------
Dim e As New ScriptControl
e.Language = "VBScript"
e.AddCode "sub main" & vbNewLine & " msgbox ref.test" & vbNewLine & "end sub"
Dim obj(4) As New toury
Dim counter As Byte
For i = 0 To 5
obj(i).test() = i
Next
Dim ref As toury
Set ref = obj(3)
e.AddObject "ref", ref, True'addobject这样使用
e.Run "main"
Set e = Nothing
End Sub
脑子转个弯嘛,哈哈
Dim e As New ScriptControl
e.Language = "VBScript"
a = 1
b = 2
c = InStr(a, b)
Debug.Print e.Eval(a + b), e.Eval(e.Eval(c))
Set e = Nothing
--------------------编程问答-------------------- eval的主要作用就是把字串转换为表达式!
所以ScriptControl的主要用途是写脚本引擎用,用这个组件,你可以写一个简单的脚本解析引擎啦,例如5楼所示 --------------------编程问答--------------------
Dim a, b
Dim e As New ScriptControl
e.Language = "VBScript"
a = "a,b,c,d,e,f,g"
b = "d"
c = "InStr(a, b)"
Set e = Nothing
InStr(a, b) 返回7
e.Eval(e.Eval(c)) 返回 0 不对。。。
--------------------编程问答-------------------- Dim a, b
Dim e As New ScriptControl
e.Language = "VBScript"
a = "a,b,c,d,e,f,g"
b = "d"
c = "InStr(a, b)"
Set e = Nothing
InStr(a, b) 返回7
e.Eval(e.Eval(c)) 返回 0 不对。。。
那位给个方法? --------------------编程问答-------------------- 哦,是我搞错了,汗~~
应该这样:
Dim a, b
Dim e As New ScriptControl
e.Language = "VBScript"
a = """a,b,c,d,e,f,g"""
b = """d"""
s = "msgbox InStr(" & a & ", " & b & ")"
e.AddCode s
Set e = Nothing
--------------------编程问答-------------------- c = "InStr(a, b)"这样写,程序根本认为 a, b就是两个"a"、"b"字串,所以=0.
a和b的值没传进去 --------------------编程问答--------------------
非常感谢,但是这样写法怎么写成function呀,
--------------------编程问答--------------------
Dim a, b--------------------编程问答-------------------- 为什么下面循环里面取不到值呢?输出是空的。
Dim e As New ScriptControl
e.Language = "VBScript"
a = """a,b,c,d,e,f,g"""
b = """d"""
s = "function myfun(a,b)" & vbNewLine & " myfun=InStr(a,b)" & vbNewLine & "end function" _
& vbNewLine & "msgbox myfun(" & a & ", " & b & ")"
e.AddCode s
Set e = Nothing
Private Sub Command1_Click()
Dim Str: Str = ""
Str = """" & tt & """"
For tt = 0 To 10
Debug.Print Eval(Str)
Next
End Sub
Public Function Eval(ByRef strEval)
Dim EvalObject As New ScriptControl
EvalObject.Language = "VBScript"
Eval = EvalObject.Eval(strEval)
Set EvalObject = Nothing
End Function
--------------------编程问答-------------------- 5楼例子写的很清楚 --------------------编程问答-------------------- 不一样,我的循环 变量(for tt=0 ...),没有办法在里面用eval执行出来。我不会VB,现在只想实现这个功能。谢谢 toury 一直解答。
补充:VB , 基础类