|
-
May 1st, 2007, 05:53 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] Me = xs.Deserialise(fs) not valid. What's up?
Hi,
I have a class that when instantiated needs to read data values from an XML file.
The file is created by a previous instance of the class, via XMLSerializer.Serialize(). No prob.
The prob is that I can't seem to read the values from within the class via XMLSerializer.Deserialize(). I can do it externally no prob.
Here's the code, which is in the class' constructor. The compiler says that 'Me' can't be the target of an assignment. I'm wondering what the solution is. Can you help?
Code:
Dim fs as New FileStream(fipData, IO.FileMode.Open)
dim xs as New Xml.Serialization.XmlSerializer(GetType(StaticObjectDefaultValues))
Me = CType(xs.Deserialize(fs), StaticObjectDefaultValues)
I had to type it by hand here, so hopefully no mistakes. 
Thx!
-
May 1st, 2007, 05:58 AM
#2
Re: [2005] Me = xs.Deserialise(fs) not valid. What's up?
Exactly as the message says: an object can't assign another object to itself. You should provide a Shared method that returns an instance of the class and perform your deserialisation there, e.g.
vb Code:
Public Shared Function Load(ByVal fipData As String)
Dim fs as New FileStream(fipData, IO.FileMode.Open)
Dim xs as New Xml.Serialization.XmlSerializer(GetType(StaticObjectDefaultValues))
Return CType(xs.Deserialize(fs), StaticObjectDefaultValues)
End Function
You can then call that method on the class to get an instance, e.g.
vb Code:
Dim sodv As StaticObjectDefaultValues = StaticObjectDefaultValues.Load("file path here")
-
May 1st, 2007, 08:49 AM
#3
Retired VBF Adm1nistrator
Re: [2005] Me = xs.Deserialise(fs) not valid. What's up?
I'd prefer doing it with a ByRef argument to the New() sub personally...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
May 1st, 2007, 09:25 AM
#4
Thread Starter
Addicted Member
Re: [2005] Me = xs.Deserialise(fs) not valid. What's up?
Thx for the replies.
jmc: Is that essentially the same as setting up a 'Static Simple Factory' pattern?
plender: Could you elaborate on that?
The Goal
---------
If it needs clarifying, I just want to assign the previously serialized values into the new instance's member variables, in its constructor. Using a Shared sub is fine, but I'd be interested in any other solutions.
Many thx!
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
|