C # University Course (fifth edition) after class exercise 17.4 serialization and de-serialization

Source: Internet
Author: User

/*17.4
(Serialization and de-serialization) modifies the previous program so that it can take advantage of class objects that can be serialized and deserialized
*/
Using System;
[Serializable]
Class Classgrade
{
public string Last {get; set;}
public string First {get; set;}
public string Id {get; set;}
public string Class {get; set;}
public string Grade {get; set;}
Public Classgrade ()
{
}
Public Classgrade (String lastName, String firstName,
String ID, String className, string grade)
{
last = LastName;
first = FirstName;
id = ID;
Class = ClassName;
Grade = Grade;
}
}
Using System;
Using System.Windows.Forms;
Using System.IO;
Using System.Runtime.Serialization;
Using System.Runtime.Serialization.Formatters.Binary;
Namespace Grades
{
public partial class Gradesform:form
{
Private FileStream fileinout;
private bool saved = false;
Private BinaryFormatter Formatter;
Public Gradesform ()
{
InitializeComponent ();
Formatter = new BinaryFormatter ();
}
private void Alltextbox_textchanged (object sender, EventArgs e)
{
Enableenterbutton ();
}
private void Saveasbutton_click (object sender, EventArgs e)
{
if (fileinout! = null)
{
Fileinout.close ();
Fileinout = null;
Statuslabel.text = "Closing file";
}
DialogResult result;
String FileName;
using (SaveFileDialog chooser = new SaveFileDialog ())
{
result = Chooser. ShowDialog ();
FileName = Chooser. FileName;
}
if (result = = DialogResult.OK)
{
if (FileName = = string. Empty)
MessageBox.Show ("Invalid file name", "Invalid file name",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Else
{
Fileinout = new FileStream (FileName,
FileMode.OpenOrCreate);
saveasbutton.enabled = false;
Saved = true;
}
Enableenterbutton ();
}
}
private void Enableenterbutton ()
{
if (lasttextbox.text! = string. Empty &&
Firsttextbox.text! = string. Empty &&
Idtextbox.text! = string. Empty &&
Classtextbox.text! = string. Empty &&
Gradetextbox.text! = string. Empty && Saved)
Enterbutton.enabled = true;
Else
enterbutton.enabled = false;
}
private void Enterbutton_click (object sender, EventArgs e)
{
string last = Lasttextbox.text;
string first = Firsttextbox.text;
string id = idtextbox.text;
string className = Classtextbox.text;
string grade = Gradetextbox.text;
Classgrade entry = new Classgrade (last, first, ID,
ClassName, grade);
Formatter. Serialize (fileinout, entry);
Statuslabel.text = "Entry saved";
Lasttextbox.clear ();
Firsttextbox.clear ();
Idtextbox.clear ();
Classtextbox.clear ();
Gradetextbox.clear ();
}
private void Loadbutton_click (object sender, EventArgs e)
{
if (fileinout! = null)
{
Fileinout.close ();
Fileinout = null;
Statuslabel.text = "Closing file";
Saveasbutton.enabled = true;
Saved = false;
}
DialogResult result;
String FileName;
using (OpenFileDialog Filechooser = new OpenFileDialog ())
{
result = Filechooser.showdialog ();
FileName = Filechooser.filename;
}
if (result = = DialogResult.OK)
{

if (FileName = = string. Empty)
MessageBox.Show ("Invalid file name", "Invalid file name",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Else
{
Fileinout = new FileStream (FileName,
FileMode.Open, FileAccess.Read);
Gradestextbox.clear ();
Try
{
while (true)
{
Classgrade entry =
(Classgrade) Formatter. Deserialize (fileinout);
Gradestextbox.appendtext (Formatentry (entry) +
"\ r \ n");
}
}
catch (SerializationException)
{
Statuslabel.text = "File loaded";
}
}
}
}
private String Formatentry (Classgrade fromfile)
{
string result = FromFile.Last.Trim () + "," +
FromFile.First.Trim () + ": \ T" + fromFile.Id.Trim () + "\ T" +
FromFile.Class.Trim () + "\ T" + FromFile.Grade.Trim ();
return result;
}
}
} using System;
Using System.Windows.Forms;
Using System.IO;
Using System.Runtime.Serialization;
Using System.Runtime.Serialization.Formatters.Binary;
Namespace Grades
{
public partial class Gradesform:form
{
Private FileStream fileinout;
private bool saved = false;
Private BinaryFormatter Formatter;
Public Gradesform ()
{
InitializeComponent ();
Formatter = new BinaryFormatter ();
}
private void Alltextbox_textchanged (object sender, EventArgs e)
{
Enableenterbutton ();
}
private void Saveasbutton_click (object sender, EventArgs e)
{
if (fileinout! = null)
{
Fileinout.close ();
Fileinout = null;
Statuslabel.text = "Closing file";
}
DialogResult result;
String FileName;
using (SaveFileDialog chooser = new SaveFileDialog ())
{
result = Chooser. ShowDialog ();
FileName = Chooser. FileName;
}
if (result = = DialogResult.OK)
{
if (FileName = = string. Empty)
MessageBox.Show ("Invalid file name", "Invalid file name",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Else
{
Fileinout = new FileStream (FileName,
FileMode.OpenOrCreate);
saveasbutton.enabled = false;
Saved = true;
}
Enableenterbutton ();
}
}
private void Enableenterbutton ()
{
if (lasttextbox.text! = string. Empty &&
Firsttextbox.text! = string. Empty &&
Idtextbox.text! = string. Empty &&
Classtextbox.text! = string. Empty &&
Gradetextbox.text! = string. Empty && Saved)
Enterbutton.enabled = true;
Else
enterbutton.enabled = false;
}
private void Enterbutton_click (object sender, EventArgs e)
{
string last = Lasttextbox.text;
string first = Firsttextbox.text;
string id = idtextbox.text;
string className = Classtextbox.text;
string grade = Gradetextbox.text;
Classgrade entry = new Classgrade (last, first, ID,
ClassName, grade);
Formatter. Serialize (fileinout, entry);
Statuslabel.text = "Entry saved";
Lasttextbox.clear ();
Firsttextbox.clear ();
Idtextbox.clear ();
Classtextbox.clear ();
Gradetextbox.clear ();
}
private void Loadbutton_click (object sender, EventArgs e)
{
if (fileinout! = null)
{
Fileinout.close ();
Fileinout = null;
Statuslabel.text = "Closing file";
Saveasbutton.enabled = true;
Saved = false;
}
DialogResult result;
String FileName;
using (OpenFileDialog Filechooser = new OpenFileDialog ())
{
result = Filechooser.showdialog ();
FileName = Filechooser.filename;
}
if (result = = DialogResult.OK)
{

if (FileName = = string. Empty)
MessageBox.Show ("Invalid file name", "Invalid file name",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Else
{
Fileinout = new FileStream (FileName,
FileMode.Open, FileAccess.Read);
Gradestextbox.clear ();
Try
{
while (true)
{
Classgrade entry =
(Classgrade) Formatter. Deserialize (fileinout);
Gradestextbox.appendtext (Formatentry (entry) +
"\ r \ n");
}
}
catch (SerializationException)
{
Statuslabel.text = "File loaded";
}
}
}
}
private String Formatentry (Classgrade fromfile)
{
string result = FromFile.Last.Trim () + "," +
FromFile.First.Trim () + ": \ T" + fromFile.Id.Trim () + "\ T" +
FromFile.Class.Trim () + "\ T" + FromFile.Grade.Trim ();
return result;
}
}
}

C # University Course (fifth edition) after class exercise 17.4 serialization and de-serialization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.