[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!
Re: Using "this" keyword properly
To hide and de-allocate
or, to simply hide:
Code:
this.Visible = false;
Re: Using "this" keyword properly
Quote:
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.
Re: Using "this" keyword properly
Quote:
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!
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. :-)
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.
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!