Hi, i recently did a post on serialize/deserialization which i have managed to do now thanks to somebody on this formum for the very helpful sample code indeed, it was apprechiated.
Basically when i want to load a new file up i deserialize, when i save a file i serialize, so serialization shouldn't happen at any other time during the program.
My program has 3 forms, form1 is an MDI container, form2 is the editing form and form3 is a form to display results.
The problem i am having is in form2 you type in a name within a textbox and then upload your own data from a file by pressing an import button.
Once you press ok the data will then be displayed in the third form but i need to add the values from form2 into a class without creating a new one.
This is the code i currently have:
import button to import external data into the program within form2:
CODE
private void button1_Click(object sender, EventArgs e)
{
//create an openfiledialog with just csv extensions
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "csv Files(*.csv)|*.csv";
ofd.Title = "Select a csv file";
ofd.ShowDialog();
if (File.Exists(ofd.FileName))
{
textBox2.Text = ofd.FileName; //tell textbox2 to equal the filename
}
}
Basically that code above allows you to import a new csv file into my program and later in you will be able to read lines from it using the streamreader etc.
the code for the second button where form2 will close and form3 will open
CODE
private void button2_Click(object sender, EventArgs e)
{
//create new instance of the class and add in the new values
SharkAdder dataclass = new SharkAdder();
dataclass.name = textBox1.Text;
dataclass.date = dateTimePicker1.Text;
dataclass.path = textBox2.Text;
this.Close();
Form3 F3 = new Form3();
F3.MdiParent = Form1.ActiveForm;
F3.Show();
}
The code above will create a new instance of the class sharkadder called dataclass, it will then add values from the current form to the class and then close the current form. Form3 is then loaded and here is some code i have for form3:
Form3 form load event:
CODE
private void Form3_Load(object sender, EventArgs e)
{
SharkAdder dataclass = new SharkAdder(); //create new instance of sharkadder class
FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read); //new file stream from path which fname points to
BinaryFormatter bf = new BinaryFormatter();
dataclass = (SharkAdder)bf.Deserialize(fs); //deserialize this
}
the above will deserialize the code from the filename given by the fname string, but here is where i have the problem:
You see when form3 loads i am creating a new instance of the sharkadder class and then deserializing the saved value.
But if you remember i already created an instance of the class in form2 so i should not need to create another one.
I only want the user to serialize the class when they go to file and save within form3. the way i have set it up, in order to get the values from the dataclass they will have to serialize before form2 closes and then deserialize when form3 loads.
Is they any way i can avoid this and store the values within the class, then call that same class from form3? At the minute i have only found a way to create a new instance and then load in the serialized data, what i need is the data to be held in the class, then i can modify it from in form3.
Yes i can do it with serializing the way i currently have it set up, but i only want to enable serialize when the person goes to save, what added code/alternative code do i use to communicate with my class to add extra data or get hold of extra data from within another form?
So it's just getting the data to remain in the class, then i can later call that class within form3 without needing to serialize it to a file first.
thanks, if i explained anything unclearly then i can explain more
This post has been edited by Sharkadder: 7 Apr, 2008 - 10:51 AM