Results 1 to 5 of 5

Thread: [RESOLVED] simply deserialze problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Resolved [RESOLVED] simply deserialze problem

    Dear friends,

    why does the deserialization not work? All Strings are empty. This sub is in my class1.
    Code:
     Public Sub deserial()
    
            Dim deserial As New XmlSerializer(GetType(Class1))
            Dim swread As New IO.StreamReader("myserialize.xml")
            deserial.Deserialize(swread)
            swread.Close()
        End Sub
    When i deserialize from outside my class1 and use this code it works perfect:

    Code:
           Dim mycl As New Class1
            Dim deseria As New XmlSerializer(GetType(Class1))
            Dim swread As New IO.StreamReader("myserialize.xml")
            mycl = CType(deseria.Deserialize(swread), Class1)
            swread.Close()
    my problem is this line of the code above:
    deserial.Deserialize(swread)

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: simply deserialze problem

    Are you getting any errors, if so, what are they? The code has a variety of things that might cause errors, but don't necessarily have to.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: simply deserialze problem

    no errors. it is this line of code:
    deserial.Deserialize(swread)

    because when i do outside of the class:
    mycl = CType(deseria.Deserialize(swread), Class1)
    it works

    but inside the class i cant use this . i tried
    ME = deserial.Deserialize(swread)
    it wont work

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: simply deserialze problem

    Quote Originally Posted by moxid View Post
    no errors. it is this line of code:
    deserial.Deserialize(swread)

    because when i do outside of the class:
    mycl = CType(deseria.Deserialize(swread), Class1)
    it works

    but inside the class i cant use this . i tried
    ME = deserial.Deserialize(swread)
    it wont work
    As you know, the Deserialize method returns an object. You have to assign that object to a variable if you want to access it later.

    So inside your class, change your deserial method to be a Shared Function that returns an instance of your class:
    VB.NET Code:
    1. Public Class Class1
    2.     'Properties
    3.  
    4.     'Subs
    5.  
    6.     'etc.
    7.    
    8.     Public Shared Function Deserial(filePath As String) As Class1
    9.         Dim deserializer As New Serialization.XmlSerializer(GetType(Class1))
    10.         Dim myClass1 As Class1
    11.  
    12.         Using swread As New IO.StreamReader(filePath)
    13.             myClass1 = CType(deserializer.Deserialize(swread), Class1)
    14.         End Using
    15.  
    16.         Return myClass1
    17.     End Function
    18. End Class
    and call it from outside your class as:
    Code:
    Dim mycl As Class1 = Class1.Deserial("myserialize.xml")

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: simply deserialze problem

    This is exactly what i was searching for. Thanks!

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