473,553 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1290
Doru Roman <do*******@roge rs.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.co m>
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*******@roge rs.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.co m>
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
4724
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 appreciated. Thanks in advance
20
3299
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 speed. I like books with practical exercises, and also with test questions (like cert books) *2*
6
2430
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 , I want to update a variable in myMainForm that keeps track of the objects that have been added.(ArrayList objectsAdded) Now I'm a little...
29
15490
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" ---Start of test.aspx---- <%@ Page language="C#" AutoEventWireup="false" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >...
7
3434
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 eventing code is slightly hidden. when you use events in Vb.Net you type this: <code> Public event MyEvent() </code>
7
3254
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 the only thing I can't migrate is my Access databases. I'm looking to export them in a standard file format, then provide convert functionality...
1
8958
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 which is a webserver running Win2k3 with IIS6, my domain account is a local administrator on this machine. The web app I am running is trying to...
1
1481
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 modified the handler and access the event. Here's an example of such (very contrived example): fucntion mousedownHandler(obj) {
2
19419
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's...
0
7568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7526
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6109
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5409
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5133
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3525
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1990
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.