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

this keyword refers to the Current Form to which the coding and controls belongs.

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.

commented: Well explained answer +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.