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
  1. Home
  2. General Programming
  3. C#
  4. how to write a object data into a file?

how to write a object data into a file?

Scheduled Pinned Locked Moved C#
questioncsharptutorial
4 Posts 3 Posters 1 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.
  • S Offline
    S Offline
    santhosh padamatinti
    wrote on last edited by
    #1

    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

    D OriginalGriffO 2 Replies Last reply
    0
    • S santhosh 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

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      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...!!

      1 Reply Last reply
      0
      • S santhosh 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

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        S 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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.

          S Offline
          S Offline
          santhosh padamatinti
          wrote on last edited by
          #4

          Thanx for valuable suggestion.

          sampath-padamatinti

          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