-
Nov 13th, 2016, 04:03 PM
#1
Thread Starter
Addicted Member
[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)
-
Nov 13th, 2016, 05:34 PM
#2
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
-
Nov 14th, 2016, 05:28 AM
#3
Thread Starter
Addicted Member
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
-
Nov 14th, 2016, 07:02 AM
#4
Re: simply deserialze problem
Originally Posted by moxid
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:
Public Class Class1
'Properties
'Subs
'etc.
Public Shared Function Deserial(filePath As String) As Class1
Dim deserializer As New Serialization.XmlSerializer(GetType(Class1))
Dim myClass1 As Class1
Using swread As New IO.StreamReader(filePath)
myClass1 = CType(deserializer.Deserialize(swread), Class1)
End Using
Return myClass1
End Function
End Class
and call it from outside your class as:
Code:
Dim mycl As Class1 = Class1.Deserial("myserialize.xml")
-
Nov 14th, 2016, 04:05 PM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|