471,350 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

How to access an object from an event?

Hi,

I have a class MyClass and in the Button1_click() event I create an instance
of that class

MyClass myc = new MyClass();

In another event I want to access myc for serialization purposes.
Let's say it is Buton2_click() event .

How do I access myc object as it is invisible in the Button2_click event?

Thanks,
Doru
Jan 24 '06 #1
4 1220
Doru Roman <do*******@rogers.com> wrote:
I have a class MyClass and in the Button1_click() event I create an instance
of that class

MyClass myc = new MyClass();

In another event I want to access myc for serialization purposes.
Let's say it is Buton2_click() event .

How do I access myc object as it is invisible in the Button2_click event?


You can't, assuming that you don't store it anywhere. If it's a local
variable, the variable only exists while the method is running.

If it's genuine state for an object, it should be an instance field of
the type of that object (eg a form).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 24 '06 #2
Thanks Jon,

Then what is the best approach?
I try to use serialization. If the object is saved I want to
unbox the object under a Form_Load event. If the object
is not saved, then I want to create the instance first under
Button1_click event and under a Button2_click I want to save it's state.
To be more descriptive, I try to simulate a playing card deck and I want to
save the cards before I close the application and the next time I open it
I need to use the same configuration of the shufled cards.
Jan 24 '06 #3
Doru Roman <do*******@rogers.com> wrote:
Then what is the best approach?
I try to use serialization. If the object is saved I want to
unbox the object under a Form_Load event. If the object
is not saved, then I want to create the instance first under
Button1_click event and under a Button2_click I want to save it's state.
To be more descriptive, I try to simulate a playing card deck and I want to
save the cards before I close the application and the next time I open it
I need to use the same configuration of the shufled cards.


Ah, right. Okay, so you need to save the contents to a file, and then
load them in the load event if they're present.

I suggest you split the task in two - first investigate serialization
(look in MSDN and use Google to find serialization tutorials), get the
serialization side of things working with the class you're interested
in, and then hook it up to the actual UI.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 24 '06 #4
OK, I got the solution: the object should be saved through serialization in
the same event where the object was created.
The same object can be retrieved in any other event afterwards.
Jan 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Frost | last post: by
29 posts views Thread by Patrick | last post: by
1 post views Thread by cpajoe2001 | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.