Jump to content
  • Advertisement
Sign in to follow this  
foolios

[.net] Accessing the timer object from one form in another.

This topic is 4007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is a fuzzy area for me. I believe it has something to do with the class not being an object but it is a gray area for me. I have a timer in one form that I stop. In another form that pops up( a new form object ) I want to restart that same timer. In that newly popped up form I use: Form1.timer1.Start(); The problem is that an object reference is required for a non-static field. I am not sure how I can go about correcting this problem. I do not understand why I can not access this timer even though it does exist. Ok, that form1 class is not an object and that timer is probably not static. So I believe it has something to do with those two issues. If someone could please enlighten me on this subject, I would really appreciate it. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
In order to access the timer like that it would have to be static within your Form1 class.

[code=CSharp]
class Form1
{
public static Timer timer1;
}


^Is an example of how to do it statically. You can modify Form1.timer1 without creating a new Form1 object. Otherwise you'd have to pass the Form1 object that you're currently working with to the new window so that it can do its stuff.

For Example:

[code=CSharp]
class Form2
{
private Form1 _parentForm
public Form2(Form1 parentForm); //<- assume this stores parentForm into _parentForm
public void DoWork()
{
_parentForm.Timer.Reset() //<- this should point to that form's timer and assume that Reset() restarts the timer. I can't remember if the timer has such a function but this is just an example.
}
}


The reason that you're getting the object reference error is that the object you're trying to work with does not exist in memory. Every time that you use "new" you're telling the program to allocate in memory everything you need for the object. Hence, until you create a new instance of the object the program has nothing to work with. Static items are automatically put into memory since they're shared by instance of a class.

So for example, the static timer I provided in the my example of Form1 is shared by every instance of Form1 you create. It doesn't matter if you create 1000 new objects of Form1 the timer is going to be the same in all of them. If this is confusing, I know that googling the subject will get you some nice graphics that tend to explain things a whole ton better.

Share this post


Link to post
Share on other sites
"private Form1 _parentForm
public Form2(Form1 parentForm); //<- assume this stores parentForm into _parentForm"

Ok, so form1 is being passed as a parameter into the constructor of Form2 right?
private Form1 parentForm, this confuses me. How did you get _parentForm and parentForm to work together like that?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!