6 Replies - 1169 Views - Last Post: 07 April 2008 - 02:45 PM Rate Topic: -----

#1 Sharkadder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-March 08

Adding data into the class wthout creating a new one

Posted 07 April 2008 - 11:50 AM

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:

		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


		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:

		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: 07 April 2008 - 11:51 AM


Is This A Good Question/Topic? 0
  • +

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

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

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!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

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

Posted 07 April 2008 - 12:44 PM

Just pass the value to the form after you create it. Like so:

	public partial class Form2 : Form {
//...
		private void button2_Click(object sender, EventArgs e) {
			// Hide, but don't die
			this.Visible = false;
			
			// create and fill your object
			SharkAdder dataclass = new SharkAdder();
			dataclass.name = textBox1.Text;
			dataclass.date = dateTimePicker1.Text;
			dataclass.path = textBox2.Text;
			
			// create form instance
			Form3 F3 = new Form3();
			F3.MdiParent = Form1.ActiveForm;
			// call a method that passes a value
			F3.SetSharkAdder(dataclass);
			// Show the form
			F3.Show();
			
			// now safe to close
			this.Close();
		}
		



	public partial class Form3 : Form {
//...
		// accept a value here, do as you like with it.
		public void SetSharkAdder(SharkAdder dataclass) {
			// process here
		}



Hope this helps.

EDIT: Or Martyr2 said :P ... damn browser refresh.

This post has been edited by baavgai: 07 April 2008 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sharkadder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-March 08

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

Posted 07 April 2008 - 12:49 PM

EDIT

sorry i didn't see your other bit of code, thanks now i see what i type in :)


ORIGINAL POST

yeah it kinda makes sense dude but i cannot find a property called DataClass :-/, i kinda don't get the method call thing.

I'm not sure what i need to set public there, do i need to set the whole form3 public in order to reach that property or what?

I typed in F3.SetDataClass(dataclass) and it doesn't recognise it, so what is it i type in form3 in order to gain access?

thanks, once this is cleared up i will know what i am doing, i tried fiddling my code but what you said i cannot seem to find within my coding.

This post has been edited by Sharkadder: 07 April 2008 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 Sharkadder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-March 08

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

Posted 07 April 2008 - 01:11 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

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.

:)
Was This Post Helpful? 0
  • +
  • -

#7 Sharkadder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-March 08

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

Posted 07 April 2008 - 02:45 PM

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 ;) )

thanks

This post has been edited by Sharkadder: 07 April 2008 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1