|
-
February 29th, 2012, 08:18 PM
#1
[RESOLVED] Using "this" keyword properly
I'm just starting out with c# and I have a splashscreen form:
Code:
namespace LottoWiz
{
public partial class SplashScreen : Form
{
public SplashScreen()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 4 * 1000; // splashscreen shows for 4 seconds.
aTimer.Enabled = true;
InitializeComponent();
}
private void SplashScreen_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
this.hide(); // how?
}
}
}
I use the system.timer, and would like to hide & deallocate the form when "OnTimedEvent" is called. But I'm able to use the "this" keyword. How would I refer to the form itself?
Thanks!
-
February 29th, 2012, 09:19 PM
#2
Re: Using "this" keyword properly
To hide and de-allocate
or, to simply hide:
Code:
this.Visible = false;
Last edited by BioPhysEngr; February 29th, 2012 at 09:19 PM.
Reason: fix tag
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
February 29th, 2012, 09:37 PM
#3
Re: Using "this" keyword properly
 Originally Posted by BioPhysEngr
To hide and de-allocate
or, to simply hide:
Code:
this.Visible = false;
That's the issue. The compiler complains when I use the "this" keyword within the "OnTimedEvent" function.
-
February 29th, 2012, 09:38 PM
#4
Re: Using "this" keyword properly
 Originally Posted by drak
That's the issue. The compiler complains when I use the "this" keyword within the "OnTimedEvent" function.
EDIT: Doh... Because the function was "static" i was unable to use "this". My bad. Thank you!
-
February 29th, 2012, 09:40 PM
#5
Re: Using "this" keyword properly
Oh, well it's because the OnTimedEvent is marked static. Static means that it pertains to the class and not to any particular instance of that class. Thus you can not reference the current instance (that is, use this) inside a static method.
Remove the static modifier in front of the OnTimdEvent method and the problem will go away.
EDIT: ah, I see you beat me to it. :-)
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
March 1st, 2012, 11:45 AM
#6
Re: [RESOLVED] Using "this" keyword properly
There really isn't any need to write 'this.' to access class properties, fields or methods.
The reason is that being a member of a class implies 'this' so explicitly typing 'this.' is just extra typing.
-
March 1st, 2012, 05:52 PM
#7
Re: [RESOLVED] Using "this" keyword properly
I tend to use 'this' when referring to member functions that I did not write, i.e. those in a class that mine is derived from. Having said that, I'm always arguing with myself over this standard. Excuse the pun!
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|