CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Object to Text

  1. #1
    Join Date
    Feb 2001
    Posts
    19

    Object to Text

    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


  2. #2

    Re: Object to Text

    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/im...ertaplanet.gif'>
    </center>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured