我有 2 个 vbs 文件。
A.vbs:
Class test
public a
public b
End Class
Run Code Online (Sandbox Code Playgroud)
B.vbs:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "C:\Users\shanmugavel.chinnago\Desktop\Test3.vbs"
Dim ins
Set ins = new test 'Here throws "Class not defined: test"
ins.a = 10
ins.b = "SCS"
msgbox ins.a
msgbox ins.b
Run Code Online (Sandbox Code Playgroud)
现在我想在 B.vbs 文件中实现这个。但是在为 A.vbs 中可用的类创建实例时会引发错误。有什么帮助吗?
. 运行.vbs 不会使代码在另一个中可用。一个简单但可扩展的策略是在“库”上使用 .ExecuteGlobal。给定的
Lib.vbs:
' Lib.vbs - simple VBScript library/module
' use
' ExecuteGlobal goFS.OpenTextFile(<PathTo\Lib.vbs>).ReadAll()
' to 'include' Lib.vbs in you main script
Class ToBeAShamedOf
Public a
Public b
End Class ' ToBeAShamedOf
Run Code Online (Sandbox Code Playgroud)
和 main.vbs:
' main.vbs - demo use of library/module Lib.vbs
' Globals
Dim gsLibDir : gsLibDir = ".\"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
' LibraryInclude
ExecuteGlobal goFS.OpenTextFile(goFS.BuildPath(gsLibDir, "Lib.vbs")).ReadAll()
WScript.Quit main()
Function main()
Dim o : Set o = New ToBeAShamedOf
o.a = 4711
o.b = "whatever"
WScript.Echo o.a, o.b
main = 1 ' can't call this a success
End Function ' main
Run Code Online (Sandbox Code Playgroud)
你会得到:
cscript main.vbs
4711 whatever
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12439 次 |
| 最近记录: |