Xan*_*rUu 2 vbscript class asp-classic
我在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
%>
Run Code Online (Sandbox Code Playgroud)
在这里我实例化一个对象并设置它的属性:
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
Run Code Online (Sandbox Code Playgroud)
问题是只有FileId得到一个值,其余的字段都是空的,即使我为它们设置了一个值.我究竟做错了什么?
Set用于将对象分配给变量.所以
Set RbacObject = New RBAC
Run Code Online (Sandbox Code Playgroud)
是正确的,但所有其他陈述都是如此
Set RbacObject.DateTime = Now
Run Code Online (Sandbox Code Playgroud)
不是.使用
RbacObject.DateTime = Now
Run Code Online (Sandbox Code Playgroud)
代替.
Set RbacObject.FileId = RS("int_fileid")
Run Code Online (Sandbox Code Playgroud)
是一个边界情况:fileIdValue将包含一个Field 对象,但在"非对象上下文"(如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'
Run Code Online (Sandbox Code Playgroud)
演示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 & "'"
Run Code Online (Sandbox Code Playgroud)
输出:
cscript 10.vbs
Object required
Set O.V => ''
O.V => 'don't do this at home.'
Run Code Online (Sandbox Code Playgroud)