Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
Hi All,
below I had given the code of C# windows base
public partial class Form1 : Form
    {
        private OleDbConnection conn = new OleDbConnection();
        private string constr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + System.Windows.Forms.Application.StartupPath.ToString() + @"\RFIDTEST.mdb" + ";Persist Security Info=False;";

        public Form1()
        {
            InitializeComponent();
            this.dataGridView.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.ck);
            conn = new OleDbConnection(constr);
            conn.Open();
            
        }

here why we put "this" key word in this line "this.dataGridView.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.ck);"
please help me to understand the code!

Thanks & Regards
Indrajit
Posted
Updated 5-Jun-11 23:36pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 16:05pm    
I up-voted this question as I don't agree with "1"; I voted "4" (which I do very rarely on questions as most of them are "sub-standard", so this is a good mark!)
--SA

You don't need to use this in this case, but you need to be aware of it and understand what is it.

How do you think a non-static (instance) method or property access the instance members? This is because the instance method has one more "hidden" parameter which is this — a reference to the instance itself. You can explicitly use is, pretty much as if you had an explicit parameter referencing some other instance:

C#
class MyClass {
    internal MyClass(MyClass other) { this.MyMember = other.MyMember; }
    static void DemoStatic(int someParameter) {
        MyStaticMember = someParameter;
        //MyMember = ... //cannot do it, there is no "this" passed to access MyMember
    }
    int MyMember;
    static int MyStaticMember; //one per whole class, only one per AppDomain
}


In the example above you don't need this. There are cases where it is a must. It happens when one name hides identical name of the member/variable:

C#
class MyClass {
    internal MyClass(int A, int B) {
        // the name not qualified with "this" takes precedence for a parameter,
        // so a member should be qualified
        this.A = A; 
        this.B = B;
    }
    int A, B;
}


[EDIT]
The run-time type of the variable passed as this becomes fundamental to OOP: when a method is virtual, this run-type type is different from the compile-time type of it which is the type of the declaring class. This opens the road to late binding and, hence, polymorphism.

See:
http://en.wikipedia.org/wiki/Late_binding[^],
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Polymorphism_(computer_science)[^].

Any questions?

—SA
 
Share this answer
 
v7
Comments
Espen Harlinn 6-Jun-11 16:17pm    
Good one, as usual - my 5
Sergey Alexandrovich Kryukov 6-Jun-11 16:25pm    
Thank you, Espen.
--SA
Monjurul Habib 6-Jun-11 16:31pm    
nice answer,my 5.
Sergey Alexandrovich Kryukov 6-Jun-11 16:40pm    
Thank you. Monjurul.
--SA
Jpuckett 6-Jun-11 17:00pm    
This might be the most complete answer to the usage of "this" that I've seen.

Might wanna take this to MSDN to replace their drab copy.
The this keyword refers to the current instance of the class. Static member functions do not have a this pointer. The this keyword can be used to access members from within constructors, instance methods, and instance accessors.

More info here[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 16:03pm    
This is correct, my 4 (because it really needs more detailed answer, I think, when to use "this"?).
Please see my solution.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900