为什么此代码段不起作用?

时间:2011-08-12 12:38:29

标签: debugging scripting vbscript

Class GetDate
    Private internal_strDate
    Private internal_strDay
    Private internal_strMonth
    Private internal_strYear
    Private internal_Debug

    Public Property Set isdebug(ByRef vLine)
        internal_Debug = vLine
        WScript.Echo("in debug mode: " & internal_Debug) 
    End Property

    Public Property Get GetFormattedDate
        internal_strDate = CDate(Date)
        internal_strYear = DatePart("yyyy", internal_strDate)
        internal_strMonth = DatePart("m", internal_strDate)
        internal_strDay = DatePart("d", internal_strDate)

        If internal_strMonth < 10 Then
            internal_strMonth = "0" & internal_strMonth
        End If
        If internal_strDay < 10 Then
            internal_strDay = "0" & internal_strDay
        End If
        GetFormattedDate = internal_strYear & "-" & internal_strMonth & "-" & internal_strDay
    End Property
End Class

在我的课程定义之后,我有了这段代码,它给了我一个错误。

Dim objYear
Set objYear = New GetDate
objYear.isdebug(True)

错误说

  

在调试模式下:False Microsoft VBScript运行时错误(68,1):对象   不支持此属性或方法:'isdebug'

基本上,我希望能够将debug设置为true,然后我将修改GetFormattedDate属性以检查'internal_Debug'是否打开,如果是,那么让我手动输入日期。 (而不是自动获得日期)

2 个答案:

答案 0 :(得分:4)

请确保您正确地对该类实例化,如下所示:

Dim objYear 
Set objYear = New GetDate
objYear.isdebug(True)

更新#1

我误读了你的代码,isdebug是一个属性,稍微修改你的类,所以“isdebug”变成了:

Public Property Let isdebug(ByRef vLine)
    internal_Debug = vLine
    WScript.Echo("in debug mode: " & internal_Debug) 
End Property

然后你就这样使用它:

objYear.isdebug = True

或者,将其更改为:

Public Sub isdebug(ByRef vLine)
    internal_Debug = vLine
    WScript.Echo("in debug mode: " & internal_Debug) 
End Sub

然后你可以像这样使用它:

objYear.isdebug(True)

答案 1 :(得分:1)

isdebug是一个属性,因此您的代码应为:

Dim objYear
Set objYear = New GetDate
objYear.isdebug = True

修改

更改

Public Property Set isdebug(ByRef vLine)

Public Property Let isdebug(ByRef vLine)

Property Set用于对象,而Property Let用于值类型。