Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Serialization problem

Serialization problem

Scheduled Pinned Locked Moved C#
helpcsharpadobebeta-testingjson
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kyoun
    wrote on last edited by
    #1

    I am working on a program to display "flash cards." Basically, the flash cards are 2 jpeg files, the front and the back. I'm using them to aid in learning a foreign language. Currently, I have this code: private void btAdd_Click(object sender, EventArgs e) { hpClass blah = new hpClass(); blah.addToArray( tbFrontFile.Text, tbBackFile.Text ); blah.serializeArray( blah, "test.txt" ); } [Serializable] public class hpClass { Stream[,] images; FileStream fsFront; FileStream fsBack; FileStream fsData; BinaryFormatter bfSerial; public void addToArray(string front, string back) { fsFront = new FileStream( front, FileMode.Open ); fsBack = new FileStream( back, FileMode.Open ); images = new Stream[1, 2] { { fsFront, fsBack } }; } public void serializeArray(object anything, string path) { fsData = new FileStream( path, FileMode.Create ); bfSerial = new BinaryFormatter(); bfSerial.Serialize( fsData, anything ); fsData.Close(); } } The error I am getting is this: Type 'System.IO.FileStream' in Assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable. And the bfSerial.Serialize line is the one generating the error. I'm using the C# 2005 Express Edition Beta. I'm completely lost :( Any suggestions?

    M 1 Reply Last reply
    0
    • K Kyoun

      I am working on a program to display "flash cards." Basically, the flash cards are 2 jpeg files, the front and the back. I'm using them to aid in learning a foreign language. Currently, I have this code: private void btAdd_Click(object sender, EventArgs e) { hpClass blah = new hpClass(); blah.addToArray( tbFrontFile.Text, tbBackFile.Text ); blah.serializeArray( blah, "test.txt" ); } [Serializable] public class hpClass { Stream[,] images; FileStream fsFront; FileStream fsBack; FileStream fsData; BinaryFormatter bfSerial; public void addToArray(string front, string back) { fsFront = new FileStream( front, FileMode.Open ); fsBack = new FileStream( back, FileMode.Open ); images = new Stream[1, 2] { { fsFront, fsBack } }; } public void serializeArray(object anything, string path) { fsData = new FileStream( path, FileMode.Create ); bfSerial = new BinaryFormatter(); bfSerial.Serialize( fsData, anything ); fsData.Close(); } } The error I am getting is this: Type 'System.IO.FileStream' in Assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable. And the bfSerial.Serialize line is the one generating the error. I'm using the C# 2005 Express Edition Beta. I'm completely lost :( Any suggestions?

      M Offline
      M Offline
      Matt Gerrans
      wrote on last edited by
      #2

      Every member of your class needs to be serializable or marked with [NonSerialized]. You cannot serialize streams, so they'll need the [NonSerialized] attribute for sure. If you really want your object to contain the binary content of all those files, you should read the data into byte array members probably. However, that sounds clunky, especially if the jpg files are right there, all your object needs to remember is their names, right? Let me know if I'm not understanding your question right. Matt Gerrans -- modified at 17:26 Thursday 13th October, 2005

      H 1 Reply Last reply
      0
      • M Matt Gerrans

        Every member of your class needs to be serializable or marked with [NonSerialized]. You cannot serialize streams, so they'll need the [NonSerialized] attribute for sure. If you really want your object to contain the binary content of all those files, you should read the data into byte array members probably. However, that sounds clunky, especially if the jpg files are right there, all your object needs to remember is their names, right? Let me know if I'm not understanding your question right. Matt Gerrans -- modified at 17:26 Thursday 13th October, 2005

        H Offline
        H Offline
        Heks
        wrote on last edited by
        #3

        Actually, I plan to use the file that is outputted as flashcard sets. Meaning it should contain all the information as the original jpegs and also on how to display them. This way, I won't have to distribute (to my friends) hundreds of jpeg files which could get moved, deleted, etc. This is why I wanted to stream the contents of the jpeg file into an array, and then save the array to a file.ext. I didn't realize you couldn't serialize streams :p So I guess I would have to dump the data into a byte array. You're right, it does sound a little clunky. I was thinking of using zip files, but then they wouldn't necesarrily come out in the right pairs (front and back.) This is the only other option I could think of. Thanks for your input :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups