Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Serialization in vb2005

Serialization in vb2005

Scheduled Pinned Locked Moved Visual Basic
json
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john_paul
    wrote on last edited by
    #1

    I have a class that I want to save to viewstate. In order to save it to viewstate it has to be serializable. How do you serialize a class and then unserialize it. thanks John

    C M N 3 Replies Last reply
    0
    • J john_paul

      I have a class that I want to save to viewstate. In order to save it to viewstate it has to be serializable. How do you serialize a class and then unserialize it. thanks John

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You mark it as serialisable, if it contains only POD, that is all you need, I believe.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • J john_paul

        I have a class that I want to save to viewstate. In order to save it to viewstate it has to be serializable. How do you serialize a class and then unserialize it. thanks John

        M Offline
        M Offline
        Marc Paliotti
        wrote on last edited by
        #3

        This book shows you how to serialize a class and deserialize it. good book

        1 Reply Last reply
        0
        • J john_paul

          I have a class that I want to save to viewstate. In order to save it to viewstate it has to be serializable. How do you serialize a class and then unserialize it. thanks John

          N Offline
          N Offline
          nlarson11
          wrote on last edited by
          #4

          John, public oObj as object . . end class Note: private variables will not be serialized Note: some variable types cannot be serialized Note: if you dont' want the value to be seralized you need to ingore the value Note: import these libraries Imports System.io Imports System.Xml Imports System.Xml.Serialization ------- To Serialize: Public Function sSerializeEntity(ByVal oEntity As Object) As String Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As New MemoryStream, bt() As Byte Try oSerializer.Serialize(oSR, oEntity) bt = oSR.GetBuffer sSerializeEntity = System.Text.Encoding.ASCII.GetString(bt) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Function ----------- To Deserialize: Public Sub DeSerializeEntity(ByVal sEntity As String, ByRef oEntity As Object) Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As MemoryStream Try oSR = New MemoryStream(System.Text.Encoding.ASCII.GetBytes(sEntity)) oEntity = oSerializer.Deserialize(oSR) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Sub --------------- ** to serlialize object ** Dim oMyEntity as new MyEntity oMyEntity.sName = "Test" dim sEntity As string = sSerializeEntity(oMyEntity) '''''''serialized''''''' you can pass the value of sEntity anywhere ** to put back into an object ** Dim oMyEntity as new MyEntity DeSerializeEntity(sEntity,oMyEntity) '''''''deserialized''''''' now you can use the object msgbox(oMyEntity.sName)

          J 1 Reply Last reply
          0
          • N nlarson11

            John, public oObj as object . . end class Note: private variables will not be serialized Note: some variable types cannot be serialized Note: if you dont' want the value to be seralized you need to ingore the value Note: import these libraries Imports System.io Imports System.Xml Imports System.Xml.Serialization ------- To Serialize: Public Function sSerializeEntity(ByVal oEntity As Object) As String Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As New MemoryStream, bt() As Byte Try oSerializer.Serialize(oSR, oEntity) bt = oSR.GetBuffer sSerializeEntity = System.Text.Encoding.ASCII.GetString(bt) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Function ----------- To Deserialize: Public Sub DeSerializeEntity(ByVal sEntity As String, ByRef oEntity As Object) Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As MemoryStream Try oSR = New MemoryStream(System.Text.Encoding.ASCII.GetBytes(sEntity)) oEntity = oSerializer.Deserialize(oSR) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Sub --------------- ** to serlialize object ** Dim oMyEntity as new MyEntity oMyEntity.sName = "Test" dim sEntity As string = sSerializeEntity(oMyEntity) '''''''serialized''''''' you can pass the value of sEntity anywhere ** to put back into an object ** Dim oMyEntity as new MyEntity DeSerializeEntity(sEntity,oMyEntity) '''''''deserialized''''''' now you can use the object msgbox(oMyEntity.sName)

            J Offline
            J Offline
            john_paul
            wrote on last edited by
            #5

            Thanks for your help

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups