对象

时间:2015-06-18 09:19:31

标签: class vbscript properties

我有一个类的开头,它最终将用于创建,检查和删除检查文件。

根据我找到的资源,以下代码似乎是正确的,但是当我运行它时,我在第4行出现错误 -

  

Object不支持此属性或方法:'Names'

我在这里做错了什么?

Dim CheckFile
Set CheckFile = new ManageCheckFile

Call CheckFile.Names("chkfile", Array("bucf", "id"), "20150618")

Class ManageCheckFile

    Private m_cfNames
    Private m_cfPrefix
    Private m_cfTypes
    Private m_cfDate

    '**
     ' Set the cfNames property
     ' Also set the cfPrefix, cfTypes and cfDate properties
     '*
    Public Property Let Names(prefix, types, date)

        m_cfPrefix = prefix
        m_cfTypes = types
        m_cfDate = date
        Dim i   ' Dummy for looping
        For i = 0 To UBound(m_cfTypes)
            m_cfNames(i) = m_cfPrefix & "-" & m_cfDate & "." & m_cfTypes(i)
        Next

    End Property

End Class

2 个答案:

答案 0 :(得分:0)

对于你正在做的事情,你可能会更好地通过一个函数而不是一个类。如果你想让它返回任何东西,你不想使用Sub。 https://msdn.microsoft.com/en-us/library/bx9ceb2w%28v=vs.84%29.aspx

答案 1 :(得分:0)

属性与变量一样使用,即使它们像过程一样被定义。您无法调用属性(Call关键字仅适用于过程和函数)。相反,您可以像这样为它们分配值:

object.property = value

此外,VBScript只允许将一个值分配给属性。属性getter和setter的其他参数用于索引访问。定义如下的属性:

Property Let Foo(a, b, c)
必须像这样使用

object.Foo(valueA, valueB) = valueC

引用documentation

[Public | Private] Property Let name ([arglist,] value)
   [statements]
   [Exit Property] 
   [statements]
End Property
     

[...]

     

arglist中

     

表示在调用时传递给 Property Let 过程的参数的变量列表。逗号分隔多个参数。 Property Let 过程中每个参数的名称必须与 Property Get 过程中的相应参数相同。此外, Property Let 过程将始终比其对应的 Property Get 过程多一个参数。该参数是分配给该属性的值。

如果必须为一个属性分配多个值,则需要将值包装在适当的数据结构(数组,字典,自定义对象等)中。例如:

Class ManageCheckFile
    Private m_cfNames
    Private m_cfPrefix
    Private m_cfTypes
    Private m_cfDate

    Public Property Let Names(data)
        m_cfPrefix = data("prefix")
        m_cfTypes  = data("types")
        m_cfDate   = data("date")

        Dim i
        ReDim m_cfNames(UBound(m_cfTypes))
        For i = 0 To UBound(m_cfTypes)
            m_cfNames(i) = m_cfPrefix & "-" & m_cfDate & "." & m_cfTypes(i)
        Next
    End Property
End Class

Set values = CreateObject("Scripting.Dictionary")
values.Add "prefix", "chkfile"
values.Add "types", Array("bucf", "id")
values.Add "date", "20150618"

Set CheckFile = New ManageCheckFile
CheckFile.Names = values

或者您使用实际方法填充多个成员变量:

Class ManageCheckFile
    Private m_cfNames
    Private m_cfPrefix
    Private m_cfTypes
    Private m_cfDate

    Public Sub Names(prefix, types, date)
        m_cfPrefix = prefix
        m_cfTypes  = types
        m_cfDate   = date

        Dim i
        ReDim m_cfNames(UBound(m_cfTypes))
        For i = 0 To UBound(m_cfTypes)
            m_cfNames(i) = m_cfPrefix & "-" & m_cfDate & "." & m_cfTypes(i)
        Next
    End Sub
End Class

Set CheckFile = New ManageCheckFile
CheckFile.Names "chkfile", Array("bucf", "id"), "20150618"

如果你把它作为类的默认方法并让它返回对象你可以(排序)在创建对象后立即初始化它:

Class ManageCheckFile
    Private m_cfNames
    Private m_cfPrefix
    Private m_cfTypes
    Private m_cfDate

    Public Default Function Names(prefix, types, date)
        m_cfPrefix = prefix
        m_cfTypes  = types
        m_cfDate   = date

        Dim i
        ReDim m_cfNames(UBound(m_cfTypes))
        For i = 0 To UBound(m_cfTypes)
            m_cfNames(i) = m_cfPrefix & "-" & m_cfDate & "." & m_cfTypes(i)
        Next

        Set Names = Me
    End Function
End Class

Set CheckFile = (New ManageCheckFile)("chkfile", Array("bucf", "id"), "20150618")

另一种选择可能是使用prefixdate作为 arglist 参数:

Class ManageCheckFile
    Private m_cfNames
    Private m_cfPrefix
    Private m_cfTypes
    Private m_cfDate

    Public Property Let Names(prefix, date, types)
        m_cfPrefix = prefix
        m_cfDate   = date
        m_cfTypes  = types

        Dim i
        ReDim m_cfNames(UBound(m_cfTypes))
        For i = 0 To UBound(m_cfTypes)
            m_cfNames(i) = m_cfPrefix & "-" & m_cfDate & "." & m_cfTypes(i)
        Next
    End Property

    Public Property Get Names(prefix, date)
        Names = m_cfNames
    End Property
End Class

Set CheckFile = New ManageCheckFile
CheckFile.Names("chkfile", "20150618") = Array("bucf", "id")

然而,除非prefixdate实际上对索引进行索引,否则这没有多大意义,这在这里似乎并非如此。根据我从代码中看到的内容,我可能会使用数据结构或默认方法路径。