Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
After staring at it about 10 minutes I finally figured out what they are doing. They are serializing an array of classes. I know that arrays are objects. I should be able to serialize an array, right?
C#
  • Leaders
Posted
To check if something is able to be serialized, you can go to the documentation and see if it has a <Serialiable> attribute on it. Just to be clear, in that example, they are serializing an array of objects in example, not an array of classes. Maybe I'm not completely understanding your question, but if you take that example and replace ClassToSerialize with String, you should get your desired effect.
--tim
Posted (edited)

It works and saves the file to disk as binary but when I try to deserialize it I get errors.

 

This is saving it:

private void SaveB_Click(object sender, System.EventArgs e)
	{
		try
		{
			string[]FormData=GetData();
			string file="CommLog\\"+DateTime.Now.ToLongDateString()+".txt";
			Stream s=new FileStream(file, FileMode.Append, FileAccess.Write);
			BinaryWriter BF=new BinaryWriter(s);
			BF.Write(FormData[0]);
			BF.Write(FormData[1]);
			/*StreamWriter s=new StreamWriter(file,true);
			s.Write(FormData[i]);*/
			s.Close();
			ClearTBs();
		}
		catch{}
	}
	
	private string[]GetData()
	{
		string[]data=new string[20];
		
		data[0]+=this.richTextBox1.Text;
		data[1]+=this.richTextBox2.Text;
		return data;
	}

 

This is attempting to deserialize:

this.NewLogB.Enabled=true; NLClosed=0;
				string file="CommLog\\"+DateTime.Now.ToLongDateString()+".txt";
				this.richTextBox1.Clear();
				string[]holdIt=new string[20];
				FileStream FS=new FileStream(file,FileMode.Open);
				BinaryReader BF=new BinaryReader();

//here is where I get the error:::			
holdIt=(string[])BF.Deserialize(FS);
				FS.Close();
				richTextBox2.Clear();
				richTextBox1.Text+=holdIt[0];
				richTextBox2.Text+=holdIt[1];

 

I cannot figure out where I messed up.

 

Here is another example modeled after the site:

this.NewLogB.Enabled=true; NLClosed=0;
				string fil="CommLog\\"+DateTime.Now.ToLongDateString()+".txt";
				File file=new File(fil);
				this.richTextBox1.Clear();
				//StreamReader SR=new StreamReader(file);
				//this.richTextBox1.Text+=SR.ReadToEnd();
				string[]holdIt=new string[20];
				//FileStream FS=new FileStream(file,FileMode.Open);
				Stream FS=file.Open(FileMode.Open);
				BinaryFormatter BF=new BinaryFormatter();
				holdIt=(string[])BF.Deserialize(FS);
				FS.Close();
				richTextBox2.Clear();
				richTextBox1.Text+=holdIt[0];
				richTextBox2.Text+=holdIt[1];

 

That does not even compile. I get an error about File and also about the arguments in file.Open.

Edited by aewarnick
C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...