Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,070 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,588 people online right now. Registration is fast and FREE... Join Now!




Adding data into the class wthout creating a new one

 
Reply to this topicStart new topic

Adding data into the class wthout creating a new one, need alternative way

Sharkadder
7 Apr, 2008 - 10:50 AM
Post #1

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 36


My Contributions
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
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Adding Data Into The Class Wthout Creating A New One
7 Apr, 2008 - 11:21 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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...

csharp

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!" decap.gif
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Adding Data Into The Class Wthout Creating A New One
7 Apr, 2008 - 11:44 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

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

csharp

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



csharp

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 tongue.gif ... damn browser refresh.


This post has been edited by baavgai: 7 Apr, 2008 - 11:46 AM
User is online!Profile CardPM
+Quote Post

Sharkadder
RE: Adding Data Into The Class Wthout Creating A New One
7 Apr, 2008 - 11:49 AM
Post #4

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 36


My Contributions
EDIT

sorry i didn't see your other bit of code, thanks now i see what i type in smile.gif


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: 7 Apr, 2008 - 11:51 AM
User is offlineProfile CardPM
+Quote Post

Sharkadder
RE: Adding Data Into The Class Wthout Creating A New One
7 Apr, 2008 - 12:11 PM
Post #5

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 36


My Contributions
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.


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Adding Data Into The Class Wthout Creating A New One
7 Apr, 2008 - 01:26 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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.

smile.gif
User is offlineProfile CardPM
+Quote Post

Sharkadder
RE: Adding Data Into The Class Wthout Creating A New One
7 Apr, 2008 - 01:45 PM
Post #7

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 36


My Contributions
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 smile.gif

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 wink2.gif )

thanks

This post has been edited by Sharkadder: 7 Apr, 2008 - 03:09 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:04PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month