PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] [2005] Me = xs.Deserialise(fs) not valid. What's up?-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Me = xs.Deserialise(fs) not valid. What's up?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    Resolved [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!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,413

    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:
    1. Public Shared Function Load(ByVal fipData As String)
    2.     Dim fs as New FileStream(fipData, IO.FileMode.Open)
    3.     Dim xs as New Xml.Serialization.XmlSerializer(GetType(StaticObjectDefaultValues))
    4.  
    5.     Return CType(xs.Deserialize(fs), StaticObjectDefaultValues)
    6. End Function
    You can then call that method on the class to get an instance, e.g.
    vb Code:
    1. Dim sodv As StaticObjectDefaultValues = StaticObjectDefaultValues.Load("file path here")

  3. #3
    Banned plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    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...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    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
  •  



Featured


Click Here to Expand Forum to Full Width