Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# load/save objects or object array

i think this is the code i use , but cannot tell without being able to load it, basically imagebook is a single object which stores "imagedata object array" containing images and integers for my load save. but have problems with "type" error on one of the lines in the load sub.

if there is a way to save the whole imagedata array and delete imagebook that would be ok too.

namespace FastCrack
{
    [Serializable]
    class ImageBook1
    {
        public ImageData1[] imgdat;
        public ImageBook1(ImageData1[] dat) {
            imgdat = new ImageData1[1000];
            imgdat = dat;
        }
    }
}




namespace FastCrack
{
    public partial class Form1 : Form
    {
        private void saveCrack()
        {      
        try
        {
              book1 = new ImageBook1(imagedataSheets);
            // Create a FileStream that will write data to file.
            FileStream writerFileStream = new FileStream(Filename1, FileMode.Create, FileAccess.Write); // Save our dictionary of friends to file
            this.formatter.Serialize(writerFileStream, this.book1); 
            writerFileStream.Close();
        }
        catch (Exception) {
            Console.WriteLine("Unable to save our friends' information");
        } // end try-catch
        }


        private void loadCrack()
        {

            if (File.Exists(Filename1))
            {

                try
                {
                    // Create a FileStream will gain read access to the 
                    // data file.
                    FileStream readerFileStream = new FileStream(Filename1,
                        FileMode.Open, FileAccess.Read);
                    // Reconstruct information of our friends from file.




  //this.friendsDictionary = (Dictionary<String, Friend> 
 //<--this is line from example

this.book1 = (Dictionary<ImageBook1, book1>) 
 // (Dictionary<String, Friend>)
    <--  this is the line giving me type error




                        this.formatter.Deserialize(readerFileStream);
                    // Close the readerFileStream when we are done
                    readerFileStream.Close();

                }
                catch (Exception)
                {
                    Console.WriteLine("There seems to be a file that contains " +
                        "friends information but somehow there is a problem " +
                        "with reading it.");
                } // end try-catch

            } // end if
        }
    }
}
like image 412
doggy Avatar asked Feb 01 '26 12:02

doggy


1 Answers

The book1 variable seems to be an ImageBook1 (not sure; you did not share its declaration with us) so you can't assign a dictionary to it.

Whatever type you serialize should also be the type being deserialized, so you probably want to cast it back to an ImageBook1:

this.book1 = (ImageBook1)this.formatter.Deserialize(readerFileStream);

If not, you'll have to edit your answer and indicate what type of dictionary you want.

like image 157
C.Evenhuis Avatar answered Feb 04 '26 01:02

C.Evenhuis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!