Results 1 to 3 of 3

Thread: Deserialising unable to cast

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Deserialising unable to cast

    Hi All,

    I have a class that I have put [Serializable] above the constructor.

    This class has an Arraylist of other objects of a different class which also has this heading.

    I have a form that holds an ArrayList of objects that are the first class.

    I can serialise using this:
    Code:
     FS = new FileStream("C:/pop_queries.dat", FileMode.Create, FileAccess.Write);
                    BinaryFormatter b = new BinaryFormatter();
                    b.Serialize(FS, common.ToArray());
                    FS.Close();
    And try to deserialise using this:
    Code:
     
    Query[] queriesFromFile;
    FS = new FileStream("C:/pop_queries.dat", FileMode.Open, FileAccess.Read);
                    BinaryFormatter b = new BinaryFormatter();
                    queriesFromFile = (Query[])b.Deserialize(FS);
                    FS.Close();
    Query is the same type as the items held in the ArrayList common, which I convert array in the serialisation!

    When I try to deserialise, I get the error:

    Unable to cast object of type 'System.Object[]' to type 'ClassifyGUI.Query[]'.

    Can anybody see where I have gone wrong?

    Sam

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Deserialising unable to cast

    Yup... when you serialized it... you serialized it as an array[] object....
    common.ToArray() <<< --- right there....

    Try just common....


    Also... why an ArrayList and not a List<T> ? what version of the FW are you using?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deserialising unable to cast

    The ArrayList.ToArray method returns an Object[]. If you want a Query[] when you call ToArray then, as tg says, you should be using List<Query> rather than an ArrayList. If that's not possible for some reason, like you're using .NET 1.x, then you will have to manually create a Query[] and copy the elements from the Object[] before serializing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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