473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 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 1283
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
20
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to...
6
by: Christian H | last post by:
Hi! I've created a custom control (myDrawControl) that deals with drawing. This control is then added to a form( myMainForm) Now, whenever something is dragged and dropped onto myDrawControl ,...
29
by: Patrick | last post by:
I have the following code, which regardless which works fine and logs to the EventViewer regardless of whether <processModel/> section of machine.config is set to username="SYSTEM" or "machine" ...
7
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the...
7
by: dtecmeister | last post by:
Looking to see how many people could use this kind of tool. I've got several large databases I've developed in Access with MySQL as the back-end. I've started using Linux instead of windows and...
1
by: cpajoe2001 | last post by:
I am having an issue and after searching around online for a day and half now and finding others with the same problem but yet no solution to my issue I am looking for help. What i have is ServerA...
1
by: VAXman- | last post by:
I have code which is created from a content mamagement system. It places some handlers on tags with in-line registration: <tag... onmousedown="mousedownHandler(this);" ...> I want to...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.