【问题标题】:Why is this code snippet not working?为什么此代码段不起作用?
【发布时间】:2011-10-25 19:02:26
【问题描述】:
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) : Object 不支持此属性或方法:'isdebug'

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

【问题讨论】:

  • 次要问题,但您的 GetFormattedDate 函数可以缩减为 GetFormattedDate = Format(Date, "yyyy-mm-dd") 并且在不同的语言环境中会更加可靠(因为它没有预先进行字符串转换)

标签: debugging scripting vbscript


【解决方案1】:

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

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)

【讨论】:

  • 天才!像 Set to Let 这样的小词产生了巨大的影响。我在哪里可以了解各种动词以及何时使用它们?
  • 试试这个,我认为它很好地解释了 Get/Set/Let:qtp.blogspot.com/2007/08/…
【解决方案2】:

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 用于值类型。

【讨论】:

  • 同样的错误信息也随之而来。我之前试过这个但是我重新测试了,因为我打错了。错误消息显示“Microsoft VBScript 运行时错误 (68, 1):对象不支持此属性或方法:'isdebug'”
  • 抱歉,我错过了您在 isdebug 属性中使用 Set 而不是 Let。我已经改变了我的答案,所以错误的答案不会永远存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
相关资源
最近更新 更多