Click to See Complete Forum and Search --> : Object to Text


Gek
September 4th, 2001, 05:04 AM
Hi there,

I'm develloping webclasses and in ASP/Webclasses it is not possible to store Objects like collections in the session object.
A way to get around this would be to read the object out of the memory, convert it to a string and then store it and the other way round when I want to read the object again.
This means I'd need to functions like Object2String and String2Object where one returns a string and the other one an object.

Is this possible in VB? If yes, could anyone get me started?

Thanks for your help,
Daniel

berta
September 4th, 2001, 05:16 AM
see if this code can help U...
in this case a save object in a file... but...


'/**********
'/* calss :myclass
Implements PropertyBag

private p as new PropertyBag

private mvarx as Single
private mvary as Single

public property let x(byval vData as Single)

mvarx = vData
mvary = mvarx + 1

p.WriteProperty "mvarx", mvarx

End property
public property get x() as Single
x = mvarx
End property

public property get y() as Single
y = mvary
End property

private property get PropertyBag_Contents() as Variant
PropertyBag_Contents = p.Contents
End property

private property let PropertyBag_Contents(byval RHS as Variant)
p.Contents = RHS
x = p.ReadProperty("mvarx", 0)
End property

private Function PropertyBag_ReadProperty(byval Name as string, optional byval DefaultValue as Variant) as Variant
'
End Function

private Sub PropertyBag_WriteProperty(byval Name as string, byval Value as Variant, optional byval DefaultValue as Variant)
'
End Sub

'/*****
'form test
option Explicit

private pb as PropertyBag
private MyObj as myclass


private Sub Form_Load()
set pb = new PropertyBag
set MyObj = new myclass
Debug.print "Loading..."
Debug.print "MyObj.x", MyObj.x
End Sub


private Sub Command1_Click()
Dim varTemp as Variant
Dim byteArr() as Byte

Dim Ipb as PropertyBag
set Ipb = MyObj

Debug.print "Reading from file..."
' Read the file contents into a Variant.
Open App.Path & "\MyObj.txt" for binary as #1
get #1, , varTemp
Close #1

' Assign the Variant to a Byte array.
'byteArr = varTemp

' Assign to the PropertyBag Contents property.
Ipb.Contents = varTemp

' Instantiate the object from the PropertyBag
'set MyObj = pb.ReadProperty("MyObj")

Debug.print "MyObj.x", MyObj.x
Debug.print "MyObj.y", MyObj.y

End Sub

private Sub Command2_Click()
Debug.print "Setting Values..."
MyObj.x = 123000
Debug.print "MyObj.x", MyObj.x
Debug.print "MyObj.y", MyObj.y
End Sub

private Sub Command3_Click()
Dim varTemp as Variant
Dim Ipb as PropertyBag
' Save the object to the PropertyBag using WriteProperty.
'pb.WriteProperty "MyObj", MyObj

set Ipb = MyObj

' Assign the Contents of the PropertyBag to a Variant.
varTemp = Ipb.Contents

Debug.print "Saving..."

' Save to a text file.
Open App.Path & "\MyObj.txt" for binary as #1
Put #1, , varTemp
Close #1

Debug.print "...File saved"
End Sub









<center>
<HR width=80%>
<img src='http://web.tiscali.it/bertaplanet/images/bertaplanet.gif'>
</center>