XandrUu
XandrUu

Reputation: 1179

VBScript class property is empty after setting a value

I made a class in VBScript and used it in asp classic to instatiate an object: This is my class:

    <%
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
 %>

And here I instantiate an object and set it's properties:

    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

The problem is that only FileId gets a value rest of the fields are empty, even if I set them an value. What am I doing wrong?

Upvotes: 2

Views: 1592

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

In addition to what Ekkehard.Horner said you need to define the setters for properties that don't take objects as

Public Property Let name(value)

not as

Public Property Set name(value)

Upvotes: 2

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Set is used for assigning objects to variables. So

Set RbacObject = New RBAC

is correct, but all other statements like

Set RbacObject.DateTime = Now

are not. Use

RbacObject.DateTime = Now

instead.

Set RbacObject.FileId = RS("int_fileid")

is a borderline case: fileIdValue will contain a Field object, but evaluate to its .Value when used in a 'non-object context' (like IO or computations).

You shouldn't run dubious code with/under On Error Resume Next.

Demo:

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 (to prove that 'it works' if you don't use Set for assignment of non-objects, and indicate that there must be other error hidden by the evil OERN if it 'still doesn't work'):

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 & "'"

output:

cscript 10.vbs
Object required
Set O.V => ''
O.V => 'don't do this at home.'

Upvotes: 6

Related Questions