1

I have read many codes on this but none happened to solve the problem. first the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Serialization
{
    class Program
    {
        static void Main(string[] args)
        {
            using (MoveSaver objSaver = new MoveSaver(@"C:\1.bin"))
            {
                MoveAndTime mv1, mv2;
                mv1.MoveStruc = "1";
                mv1.timeHLd = DateTime.Now;
                objSaver.SaveToFile(mv1);
                mv2.MoveStruc = "2";
                mv2.timeHLd = DateTime.Now;
                objSaver.SaveToFile(mv2);
            }

            using (MoveSaver svrObj = new MoveSaver())
            {
               MoveAndTime[] MVTobjs = svrObj.DeSerializeObject(@"C:\1.bin");
               foreach (MoveAndTime item in MVTobjs)
               {
                   //Do Something
               }
            }
        }

    }


    public class MoveSaver:IDisposable 
    {
        public void Dispose()
        {
            if (fs != null)
            {
                fs.Close();
            }
        }
        FileStream fs;
        StreamWriter sw;
        public string filename { get; set; }
        public MoveSaver(string FileName)
        {
            this.filename = FileName;
            fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        }

        public MoveSaver()
        {

        }

        ~MoveSaver()
        {
            if (fs != null)
            {
                fs.Close();
            }

        }

        public MoveAndTime[] DeSerializeObject(string filename)
        {
            MoveAndTime[] objectToSerialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            objectToSerialize = (MoveAndTime[])bFormatter.Deserialize(stream);
            stream.Close();
            return objectToSerialize;
        }

        public bool SaveToFile(MoveAndTime moveTime)
        {
            try
            {
                BinaryFormatter bformatter = new BinaryFormatter();
                bformatter.Serialize(fs, moveTime);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }

    [Serializable]
    public struct MoveAndTime
    {
        public string MoveStruc;
        public DateTime timeHLd;
    }
}

The code mimics a need for saving all actions of user on the program. to be later shown on that program (say you play cards or so and you want to review :D what has happened). The problem is when DeSerializeObject function is called the line objectToSerialize = (MoveAndTime[])bFormatter.Deserialize(stream); throws an exception (definitely in runtime) that the cast from a single object to array is not valid:

Unable to cast object of type 'Serialization.MoveAndTime' to type 'Serialization.MoveAndTime[]'.

Any idea? Any improvement or total change of approach is appreciated.

1
  • 1
    You should not have a finalizer Commented Feb 11, 2011 at 14:53

2 Answers 2

1

You're saving a single MoveAndTime instance to the file, but you're trying to read an array of them.

Sign up to request clarification or add additional context in comments.

7 Comments

how can I save an array when the inputs are discontinued and one at a time?
Are you trying to write multiple objects one-at-a-time, then write them as an array?
Yeah exactly. The main Problem is this.
You can either save a complete array to the file every time, or read them one by one and add them to a List<T>.
How can I read them one by one? if I don't want to change the way I store them in a single file, is it possible to do that? Can you provide me with a sample code?
|
1

Please modify your main block like this. I think it achieves what you want.

static void Main(string[] args)
{
        using (MoveSaver objSaver = new MoveSaver(@"C:\1.bin"))
        {
            MoveAndTime[] MVobjects = new MoveAndTime[2];

            MoveAndTime mv1, mv2;
            mv2 = new MoveAndTime();
            mv1 = new MoveAndTime();
            mv1.MoveStruc = "1";
            mv1.timeHLd = DateTime.Now;

            mv2.MoveStruc = "2";
            mv2.timeHLd = DateTime.Now;

            MVobjects[0] = new MoveAndTime();
            MVobjects[0] = mv1;
            MVobjects[1] = new MoveAndTime();
            MVobjects[1] = mv2;

            objSaver.SaveToFile(MVobjects);
        }

        using (MoveSaver svrObj = new MoveSaver())
        {
            MoveAndTime[] MVTobjs = svrObj.DeSerializeObject(@"C:\1.bin");
            foreach (MoveAndTime item in MVTobjs)
            {
                //Do Something
                Console.WriteLine(item.MoveStruc);
                Console.WriteLine(item.timeHLd);
            }
        }
    }

Thanks

1 Comment

Thanks for your code, but I have scattered data and they are available one at a time, so I can't pass an array that already has all data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.