gpt4 book ai didi

vbscript - 从另一个 .VBS 文件创建类的实例(驻留在 B.vbs 中)

转载 作者:行者123 更新时间:2023-12-02 21:41:36 26 4
gpt4 key购买 nike

我有 2 个 vbs 文件。

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 中可用的类创建实例时会抛出错误。有什么帮助吗?

最佳答案

.运行.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

和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

(参见 this answer 有用类的种子)

关于vbscript - 从另一个 .VBS 文件创建类的实例(驻留在 B.vbs 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10614492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com