从另一个 .VBS 文件为类(驻留在 B.vbs 中)创建实例 [英] Create instance for a class(resides in B.vbs) from another .VBS file
本文介绍了从另一个 .VBS 文件为类(驻留在 B.vbs 中)创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个 vbs 文件.
I have 2 vbs files.
A.vbs
:
Class test
public a
public b
End Class
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
现在我想在 B.vbs 文件中实现这个.但是在为 A.vbs 中可用的类创建实例时会引发错误.有什么帮助吗?
Now I want to achive this like in B.vbs file. But it throws error while creating instance for the class availble in A.vbs. Any help?
推荐答案
.运行一个 .vbs 不会使代码在另一个中可用.一个简单但可扩展的策略是在库"上使用 .ExecuteGlobal.给定
.Running a .vbs won't make the code usable in another one. A simple but extensible strategy is to use .ExecuteGlobal on the 'libraries'. Given
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
和 main.vbs:
and 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
你会得到:
cscript main.vbs
4711 whatever
(参见这个答案以获得有用类的种子)
(cf. this answer for a seed of a useful class)
这篇关于从另一个 .VBS 文件为类(驻留在 B.vbs 中)创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文