0

Hi

I am new to programming and to the c# syntax. Can someone explain to me what this refers to in the snippet of code below

using System;
using System.Windows.Forms;

class MyTestButtonClass : Form
{
    private Button myButton;

    // Constructor method

    public MyTestButtonClass()
    {
        myButton = new Button();
        myButton.Text = "Click me";
        this.Controls.Add(myButton);
    }

    // Main method

    static void Main()
    {
        Application.Run(new MyTestButtonClass());
    }
}

Is this a new Form or is this a new instance of the MyButtonClass or if I am totally off the point can someone explain the code to me. I would have thought that the following bit of code

Application.Run(new MyTestButtonClass());

should be made outside of the class definition??? Thanks in advance for all your help

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Momerath
1

To expand on that, this refers to the current instance of the enclosing class. In your code example it would refer to the instance of MyTestButton that would be executing that code.

Votes + Comments
Well explained answer
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.