CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2012
    Posts
    3

    [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!

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Using "this" keyword properly

    To hide and de-allocate

    Code:
    this.close();
    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.

  3. #3
    Join Date
    Feb 2012
    Posts
    3

    Re: Using "this" keyword properly

    Quote Originally Posted by BioPhysEngr View Post
    To hide and de-allocate

    Code:
    this.close();
    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.

  4. #4
    Join Date
    Feb 2012
    Posts
    3

    Re: Using "this" keyword properly

    Quote Originally Posted by drak View Post
    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!

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  7. #7
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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
  •  





Click Here to Expand Forum to Full Width

Featured