设置值后,VBScript类属性为空

时间:2013-05-15 06:31:59

标签: class asp-classic vbscript

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

    <%
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 获取值,其余字段为空,即使我为它们设置了值。我做错了什么?

2 个答案:

答案 0 :(得分:6)

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

Set RbacObject = New RBAC

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

Set RbacObject.DateTime = Now

不是。使用

RbacObject.DateTime = Now

代替。

Set RbacObject.FileId = RS("int_fileid")

是一个临界案例:fileIdValue将包含一个Field 对象,但在“非对象上下文”(如IO或计算)中使用时会计算其.Value。< / p>

你不应该在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'

演示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.'

答案 1 :(得分:2)

除了Ekkehard.Horner所说的你需要为不接受对象的属性定义setter

Public Property Let name(value)

不是

Public Property Set name(value)