3

I have defined a class like this:

Class Foo
    Public SomePublicProperty

    Public Function init(p_somePublicProperty)
        set init = Me

        SomePublicProperty= p_somePublicProperty
    End Function
End Class

And then consumed that in my global.asa Application_OnStart lke this:

Dim fooInstance 

Set fooInstance = New Foo.init("Some data")

fooArray = Array(fooInstance)

Application("fooArray") = fooArray

Which works fine but when i get the value back out of the application store on another page i can't get at the property...

fooArray = Application("fooArray")

fooArray(0).SomePublicProperty 'This line returns an error - Object doesn't support this property or method

I have tried putting the class definition into the second page, but it doesn't help.

What have I missed?


I have just found this question. Am I right in assuming the same rule re serialization applies equally to the Application object? and so i shouldn't try and do this?

Community
  • 1
  • 1
jaybeeuu
  • 1,013
  • 10
  • 25

1 Answers1

0

Unfortunately you can't do this, here is a the best explanation I could find as to why;

From Can I store VBScript class objects in a Session variable?
This is because VBS classes are NOT true classes. They are really just in-memory collections of info, and there is no way to guarantee (for example) that an instance that is stored in one page will even come close to matching the class definition in another page.

This is not the same as using Server.CreateObject() COM objects which can be stored and retrieved from both the Application and Session objects.

You have a couple of options;

  • Serialise the object yourself, in a structured string then use this to de-serialise the object when needed.

  • Create a COM wrapper for your VBScript class and stop using Class statement altogether. As COM objects can be stored in Application and Session objects this should work as long as the COM class is single threaded.

  • Convert your class into an Array and use this instead.


Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175