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:

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.

Replies To: Adding data into the class wthout creating a new one

Re: Adding data into the class wthout creating a new one

Posted 07 April 2008 - 12:21 PM

All you have to do is pass the class you created in form2 to form3 through a public method in form3.

So in form3 you can add a variable to hold the class created in form2. This will be a form level variable of the type SharkAdder. Then you need to create a public method in form3 which takes as a parameter a SharkAdder object. This method (or could be a property if you choose) can then take in the object, even do validation on it to make sure it is correct, and set its private member variable equal to the object passed in.

Make sense? Remember that forms in .NET are classes themselves. So you can add a method to the form which takes parameters.

In form 2 when you go to launch form3 you would then do something like...

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;
// If you choose to pass it using a method
F3.SetDataClass(dataclass);
// OR if you want to use it as a property
F3.DataClass = dataclass;
F3.Show();
}

So as you can see above we are doing both methods, but you will choose one depending if you want to create it as a method or as a property. The method or property you define for it back on F3 must be defined as public so that we can have access to it and then take in one parameter, a parameter of type SharkAdder.

Hopefully this is making sense. Enjoy!

"At DIC we be passing SharkAdders around like true code ninjas... we also pass around the beer pretty good too!"

Re: Adding data into the class wthout creating a new one

ok i'm almost done just one last thing, when i serialize the class when i go to save the file, i have a slight problem.

What paramater do i pass to the serialize? currently i have this:

bf.Serialize(fs, dataclass);

Since i am serializing from within form 3, i need another way of of getting the value from the class, the dataclass is not recognised and i tried passing it SharkAdder dataclass but it wouldn't have it.

Re: Adding data into the class wthout creating a new one

Posted 07 April 2008 - 02:26 PM

Remember me mentioning creating a variable on form3 to hold the value passed to the form from form2? That is your class variable you can serialize. Since you are serializing from form3 when they save, you have access to that form level variable because it belongs to form3.

Re: Adding data into the class wthout creating a new one

ok i'm not sure if what i have done is correct but this is what i now have:

When the person loads up a file, at the end of starting a new form3 i inset the data like this:

F3.label1.Text = dataclass.name;
F3.label2.Text = dataclass.date;

i tried inputting that data within the SetSharkadder method on form3 but that only works when going from form2 to form3. I persume that the best way to do it what i have done, if it's wrong and i can include it within that method then i couldn't figure how to do that.

Next was to work out how to serialize properly, i used this command when you press the ok button in form2:

F3.dataclass = dataclass

That allowed a variable in form3 to equal the same value. In form3 i put this code in the partial class:

public SharkAdder dataclass;

Now when i go to serialize the object through file and save, the project then serializes properly.

The only thing i am abit unsure of though is the way i put the elements back into form3, so i put in this code instead of the F3.label1 = whatever code i first used when i open the saved file:

F3.SetSharkadder(dataclass);
F3.dataclass = dataclass;

that way all the data gets put into the right place in form3.

Is what i have done the best way to do it?

It works put it that way, i know they say once you fdound asolution don't try and solve it but i like things to be neat, to me it looks the best way to do it anyways

EDIT

Seems to of done the trick, no worries dude, as everything else, any problems i have i will bring the topic up again with a new post. Thanks for the help once again. Now that my imported files data shows on a textbox, i can now do some calculations with my data results, hehehehe. (keeps me ocupied tomorrow )