Persist Object
<script language="vbscript" runat=server>
class persist
'***********************************************************************************
'*** 04/12/02 TOM LANG Persist Object
'*** vbscript wrapper class used to save form information
'***********************************************************************************
' *** USER MANUAL
'***********************************************************************************
'set objPersist = new persist 'init object
'objPersist.nameSpace = "myAppName" 'to prevent name collisions with other sub apps
'objPersist.store name, value
'value=objPersist.fetch name
'set ovalue=objPersist.fetch oName 'gets an object
'objPersist.clear name 'set value to empty string
'objPersist.remove name 'removes name from collection
'objPersist.removeAll 'removes all info from collection
'set objPersist=nothing
'***********************************************************************************
'Note: error handling is light due to the relative stability of working with session
'variables. Error handling should proBably be added if switching the data store
'to a file or dB.
'***********************************************************************************
'Private members
private m_strErrorDescription
private m_obj 'this obj will be persisted
private m_strNameSpace 'namespace to prevent variable name collision
'class initialization
private sub class_initialize
m_strNameSpace="pso_" 'default namespace
m_strErrorDescription=""
end sub
private sub class_terminate
'code to be run when obj is set to nothing
end sub
'Public interface
public function store(key, value)
'store information contained in parameter
if isobject(value) then
set session(m_strNameSpace & key)=value
else
session(m_strNameSpace & key)=value
end if
end function
public function fetch(key)
'retreive info from data store (use instead of retrieve)
if isobject(session(m_strNameSpace & key))=true then
set fetch=session(m_strNameSpace & key)
else
fetch=session(m_strNameSpace & key)
end if
end function
public function retreive(key)
'same as fetch. kept to be compatible with previous vs.
'NEW development should use fetch method
if isobject(session(m_strNameSpace & key))=true then
set retreive=session(m_strNameSpace & key)
else
retreive=session(m_strNameSpace & key)
end if
end function
public function removeAll()
'removes all items from existence
for each itm in session.Contents
if cStr(ucase(left(itm, len(m_strNameSpace))))=cStr(ucase(m_strNameSpace)) then
if isobject(session(itm))=true then
set session(itm)=nothing 'removes item from memory
end if
Session.Contents.Remove(itm) 'removes it from collection
end if
next
end function
public function remove(key)
on error resume next
if isobject(session(m_strNameSpace & key))=true then
set session(m_strNameSpace & key)=nothing 'removes item from memory
end if
Session.Contents.Remove(cStr(m_strNameSpace & key)) 'removes it from collection
m_strNameSpace=Err.Description
end function
public function clear(key)
'set key to empty string
if isobject(session(m_strNameSpace & key)) then
set session(m_strNameSpace & key)=nothing
end if
session(m_strNameSpace & key)=""
end function
public property let nameSpace (strNameSpace)
'prevent variable collisions by providing (sub)application name
m_strNameSpace=strNameSpace
end property
public property get errorDescription
'user friendly error message
errorDescription = m_strErrorDescription
end property
end class
</script>
Submitted by Tom Lang