how to write a object data into a file?
-
Hi, I am new to C#, I have a doubt please tell me the answer. I have a form1. In that I am retrieving student information(studentgroup, studentname, admintionnumber). Now
namespace student_information { public partial class Form1 : Form { string m_stname; string m_sgroup; int m_iadminnum; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { m_stname = this.textBox1.Text; m_iadminnum = int.Parse(this.textBox2.Text); m_sgroup = this.comboBox1.SelectedItem.ToString(); Form1 f1 = new Form1(); f1.m_iadminnum = m_iadminnum; f1.m_sgroup = m_sgroup; f1.m_stname = m_stname; } } }
In the above code what i did is i am receiving information from form1, Now that information in f1(object). Can any one tell what is the procedure for store this data into a file. Also please tell me the procedure for retrieving object information from the same file. Thanx in advance......sampath-padamatinti
-
Hi, I am new to C#, I have a doubt please tell me the answer. I have a form1. In that I am retrieving student information(studentgroup, studentname, admintionnumber). Now
namespace student_information { public partial class Form1 : Form { string m_stname; string m_sgroup; int m_iadminnum; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { m_stname = this.textBox1.Text; m_iadminnum = int.Parse(this.textBox2.Text); m_sgroup = this.comboBox1.SelectedItem.ToString(); Form1 f1 = new Form1(); f1.m_iadminnum = m_iadminnum; f1.m_sgroup = m_sgroup; f1.m_stname = m_stname; } } }
In the above code what i did is i am receiving information from form1, Now that information in f1(object). Can any one tell what is the procedure for store this data into a file. Also please tell me the procedure for retrieving object information from the same file. Thanx in advance......sampath-padamatinti
You can serialize the data using XMLSerializer class and write that to the file. You can deserialize the same to read it back into the object.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
Hi, I am new to C#, I have a doubt please tell me the answer. I have a form1. In that I am retrieving student information(studentgroup, studentname, admintionnumber). Now
namespace student_information { public partial class Form1 : Form { string m_stname; string m_sgroup; int m_iadminnum; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { m_stname = this.textBox1.Text; m_iadminnum = int.Parse(this.textBox2.Text); m_sgroup = this.comboBox1.SelectedItem.ToString(); Form1 f1 = new Form1(); f1.m_iadminnum = m_iadminnum; f1.m_sgroup = m_sgroup; f1.m_stname = m_stname; } } }
In the above code what i did is i am receiving information from form1, Now that information in f1(object). Can any one tell what is the procedure for store this data into a file. Also please tell me the procedure for retrieving object information from the same file. Thanx in advance......sampath-padamatinti
private void button1_Click(object sender, EventArgs e)
{
m_stname = this.textBox1.Text;
m_iadminnum = int.Parse(this.textBox2.Text);
m_sgroup = this.comboBox1.SelectedItem.ToString();Form1 f1 = new Form1();
f1.m_iadminnum = m_iadminnum;
f1.m_sgroup = m_sgroup;
f1.m_stname = m_stname;
}While what you have will work, it is not exactly a good idea. When you create the new instance of Form1
Form1 f1 = new Form1();
you do not just allocate space for m_stname, m_iadminnum, and m_sgroup, you are also creating new instances of textBox1, textBox2, and comboBox1 as well as a whole load of other stuff you can't see - which is not exactly efficient or usefull. Consider creating a separate class "StudentInfo" to hold your records, which has the name, admin number, and group. You can then give that class a constructor which takes the three parameters, and a Save/Load method which keeps the data neatly encapsulated. Additional style points: 1) m_stname, m_iadminnum, and m_sgroup are no longer recommended for C# - consider studentName, adminNumber and studentGroup instead. 2) Don't call you controls by the default name: textBox1 is not as usefull as tbStudentName, button1 is less helpfull than butSave and so on. This may not make a big difference now, but when your app gets more complex (and it will) it makes life a whole lot easier. 3) In your button1_Click event, you do not need to prefix your control names with "this." - it is implied. The only time you should have to use "this" is when you have a parameter with the same name as a field. 4) Unless there is a very good reason for it, make your fields private - you can always add a public property if you need to expose them outside your class. Trust me, it is worth getting these things right from the start - it is a whole lot easier than breaking bad habits later!
All those who believe in psycho kinesis, raise my hand.
-
private void button1_Click(object sender, EventArgs e)
{
m_stname = this.textBox1.Text;
m_iadminnum = int.Parse(this.textBox2.Text);
m_sgroup = this.comboBox1.SelectedItem.ToString();Form1 f1 = new Form1();
f1.m_iadminnum = m_iadminnum;
f1.m_sgroup = m_sgroup;
f1.m_stname = m_stname;
}While what you have will work, it is not exactly a good idea. When you create the new instance of Form1
Form1 f1 = new Form1();
you do not just allocate space for m_stname, m_iadminnum, and m_sgroup, you are also creating new instances of textBox1, textBox2, and comboBox1 as well as a whole load of other stuff you can't see - which is not exactly efficient or usefull. Consider creating a separate class "StudentInfo" to hold your records, which has the name, admin number, and group. You can then give that class a constructor which takes the three parameters, and a Save/Load method which keeps the data neatly encapsulated. Additional style points: 1) m_stname, m_iadminnum, and m_sgroup are no longer recommended for C# - consider studentName, adminNumber and studentGroup instead. 2) Don't call you controls by the default name: textBox1 is not as usefull as tbStudentName, button1 is less helpfull than butSave and so on. This may not make a big difference now, but when your app gets more complex (and it will) it makes life a whole lot easier. 3) In your button1_Click event, you do not need to prefix your control names with "this." - it is implied. The only time you should have to use "this" is when you have a parameter with the same name as a field. 4) Unless there is a very good reason for it, make your fields private - you can always add a public property if you need to expose them outside your class. Trust me, it is worth getting these things right from the start - it is a whole lot easier than breaking bad habits later!
All those who believe in psycho kinesis, raise my hand.
Thanx for valuable suggestion.
sampath-padamatinti