2

我在 VBScript 中创建了一个类,并在 asp classic 中使用它来实例化一个对象:这是我的类:

    <%
Class RBAC

    Public dateTimeValue
    Public userIdValue
    Public fileIdValue
    Public actionValue

    Public Property Get DateTime()
            'Gets the propery value
            DateTime = dateTimeValue
        End Property    
    Public Property Set DateTime(value)
            'Sets the property value
            dateTimeValue = value
    End Property

    Public Property Get UserId()
            'Gets the propery value
            UserId = userIdValue
        End Property
    Public Property Set UserId(value)
            'Sets the property value
            userIdValue = value
    End Property

    Public Property Get FileId()
            'Gets the propery value
            FileId = fileIdValue
        End Property
    Public Property Set FileId(value)
            'Sets the property value
            fileIdValue = value
    End Property

    Public Property Get Action()
            'Gets the propery value
            Action = actionValue
        End Property
    Public Property Set Action(value)
            'Sets the property value
            actionValue = value
    End Property

    Public Sub Insert()
        sqlMethods = "INSERT INTO RBAC ([DateTime],[UserId],[FileId],[Action]) VALUES ("+dateTimeValue+","+userIdValue+","+fileIdValue+","+actionValue+",)"
        Conn.Execute(sqlMethods)
    End Sub

End Class
 %>

在这里我实例化一个对象并设置它的属性:

    Dim RbacObject
Set RbacObject = New RBAC
Set RbacObject.DateTime = Now
Set RbacObject.UserId = Cstr(Session("cgsid"))
sqlFileId = "SELECT int_fileid FROM tbl_SecFiles where str_filename = '"&split(Request.ServerVariables("SCRIPT_NAME"),"/cgs/")(1)&"'"
Set RS = Conn.Execute(sqlFileId)
Set RbacObject.FileId = RS("int_fileid")
Set RbacObject.Action = "<"&stringMethods&"><old>"&enabled_profiles_old&"</old><new>"&enabled_profiles_old&siteid&",</new></"&stringMethods&">"

RbacObject.Insert

问题是只有FileId获得一个值,其余字段为空,即使我为它们设置了一个值。我究竟做错了什么?

4

2 回答 2

6

Set用于将对象分配给变量。所以

Set RbacObject = New RBAC

是正确的,但所有其他陈述如

Set RbacObject.DateTime = Now

不是。利用

RbacObject.DateTime = Now

反而。

Set RbacObject.FileId = RS("int_fileid")

是一个临界情况:fileIdValue将包含一个 Field object,但在“非对象上下文”(如 IO 或计算)中使用时评估为它的 .Value 。

您不应该使用 /under 运行可疑代码On Error Resume Next

演示

copy con 10.vbs
Class C
 Public V
End Class
Set O = New C
Set O.V = "don't do this at home."
^Z

cscript 10.vbs
... 10.vbs(5, 1) Microsoft VBScript runtime error: Object required: 'O.V'

Demo II(证明如果你不Set用于非对象的赋值“它有效”,并表明如果它“仍然无效”则必须有其他错误被邪恶的OERN隐藏):

Class C
 Public V
End Class
Set O = New C
On Error Resume Next
Set O.V = "don't do this at home."
WScript.Echo Err.Description
On Error GoTo 0
WScript.Echo "Set O.V => '" & O.V & "'"
O.V = "don't do this at home."
WScript.Echo "O.V => '" & O.V & "'"

输出:

cscript 10.vbs
Object required
Set O.V => ''
O.V => 'don't do this at home.'
于 2013-05-15T07:05:12.923 回答
2

除了Ekkehard.Horner所说的之外,您还需要为不将对象作为对象的属性定义设置器

Public Property Let name(value)

不像

Public Property Set name(value)
于 2013-05-15T10:01:21.893 回答