如何使用数组的 Property Let?

发布于 2024-11-23 15:18:58 字数 677 浏览 4 评论 0原文

我对 VBS 很陌生,但看起来我无法实现最简单的事情。我想要一个在私有成员中保存数组的类。由于我想“注入”数组,因此我尝试使用 Let 功能实现“setter 方法”。

Class CPhase

    Private m_AllowedTasks()

    Public Property Let AllowedTasks(p_AllowedTasks)
        m_AllowedTasks = p_AllowedTasks
    End Property

    Private Sub Class_Initialize()
        ReDim m_AllowedTasks(0)
End Sub
End Class

此类的使用方式如下:

Dim allowed
allowed = Array("task1", "task2")
Dim phase
Set phase = New CPhase 
phase.AllowedTasks = allowed

这会导致 Let 方法中出现“Microsoft VBScript 运行时错误 (...):类型不匹配”。我还尝试使用“ByVal”、“ByRef”的不同组合,但由于完全没有 VBS 经验,我找不到解决方案。那么我做错了什么?

非常感谢任何有用资源的提示或链接! 谢谢!

I am very new to VBS, but I am not able to implement even the simplest things, as it seems. I want to have a class which holds an array in a private member. Since I want to "inject" the array I tried to implement a "setter-method" using the Let functionality.

Class CPhase

    Private m_AllowedTasks()

    Public Property Let AllowedTasks(p_AllowedTasks)
        m_AllowedTasks = p_AllowedTasks
    End Property

    Private Sub Class_Initialize()
        ReDim m_AllowedTasks(0)
End Sub
End Class

This class is used as follows:

Dim allowed
allowed = Array("task1", "task2")
Dim phase
Set phase = New CPhase 
phase.AllowedTasks = allowed

This results in a "Microsoft VBScript runtime error (...) : Type mismatch" in the Let-method. I also tried using different combinations of "ByVal", "ByRef", but since having absolutely no experience with VBS I couldn't find a solution. So what am I doing wrong?

Any hints or links to helpful ressources are very appreciated!
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-11-30 15:18:58

罪魁祸首是

Private m_AllowedTasks()

它创建了一个令人厌恶的东西 - 一个没有大小的固定数组。只需删除()即可。

Private m_AllowedTasks

创建一个(空)变体,可以将其 set=let 为有用的(可重整)数组。

The culprit is

Private m_AllowedTasks()

which creates an abomination - a fixed array of no size. Just remove the ().

Private m_AllowedTasks

to create an (empty) Variant that may be set=let to an useful (redim-able) array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文