vbscript执行:退出外部For

时间:2016-01-21 21:49:35

标签: vbscript

vbscript中的

执行用于将字符串解析为内联指令。但是,我不能用它来打破 For 循环。以下内容不起作用:

For i = 1 To 10
    msgbox i
    Execute("Exit For")
Next

有人知道它有效吗?或退出子/退出功能的任何方式工作?我尝试过ExecuteGlobal,但它也引发了“无效退出指令”错误。

我在整个代码中使用它作为“导入”的一种形式,再次实现另一种导入形式将耗费时间,所以我在寻找一种方法使其工作,然后才考虑更改项目

编辑:更多示例

简而言之,我尝试对半抽象方法进行部分覆盖,但不能对执行块外的For使用退出

Dim names_array 'this array will be populated in another Sub

'this method will loop though all elements of an array and insert some items into a database...
'but the method does not know (how to)/(if it will really) insert into the database!
'all it knows is: it has to loop though the array... and, if needed, finish earlier
'the insertion part will have to be described before running, like a 'partial override' of an abstract method
'that is because I know how part of the method must be, but the details will be diferent for each database
'I don't want to override every method entirely!
Sub insert_into_database
    For i = 0 To UBound(names_array)
        name = names_array(i)
        Execute(core_op) 'here comes the core operation, it is what I override!
    Next
End Sub

'for some databases, core_op will be simple:
core_op = "objRecordSet.Open ""INSERT INTO videos (title, date_time) VALUES ('"" & name & ""', Now)"", objConnection"

'for others, it may be complex:
core_op =           "If name=""18+ only"" Then"
core_op = core_op + "    MsgBox ""You tried to insert nasty things. System will now halt."""
core_op = core_op + "    Exit For"
core_op = core_op + "Else"
core_op = core_op + "    objRecordSet.Open ""INSERT INTO videos (title, date_time) VALUES ('"" & name & ""', Now)"", objConnection"
core_op = core_op + "End If"
'the string won't be loaded this way, it will come from an external file
'there are other methods that operate like this, but not all of them need Exit For

这更接近我的项目。实际情况是:覆盖的代码将来自外部文件,并将存储在字典中。

很抱歉,如果它看起来一团糟,那就是我试图让vbscript更像java的可重用性。

1 个答案:

答案 0 :(得分:0)

Exit语句是复合语句(循环,例程)的一部分,必须在其上下文中“编译”。因此,没有解决方法,您必须重新设计导入策略。也许您应该发布您当前的方法。

更新wrt评论(方法的部分覆盖):

使用GetRef并传递“函数指针”,或投资某些类并将对象发送到封闭函数。