Don't get the use of the "this" keyword in this instance

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 757 Views - Last Post: 03 February 2020 - 03:48 PM Rate Topic: -----

#1 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 22-February 17

Don't get the use of the "this" keyword in this instance

Posted 27 January 2020 - 06:02 PM

Examples of the "this" keyword I've come across and am ok with:

1. extension methods:
public static class extensionmethod_name (this datatype variable, paramters1, parameter 2, etc.)

2. Use of this to avoid naming conflicts and thus indicates current instance, for example:

        int a = 1; // These will be instance variables once this class
        int b = 2; // is instantiated in the Main Program 

        public void IntFunction(int a, int B)/>/>/> 
        {
            this.a = 2*a; //value of int a declared above is now multiplied with 2*a where latter 
            this.b = 2*b; //a passed into method as a parameter.
        }



I get the above two but not this one:

3. plusCommand = new PlusCommand(this)

where PlusCommand is a class defined elsewhere. The above says to me that plusCommand is a new instance of PlusCommand. But what does the (this) by itself in brackets mean?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Don't get the use of the "this" keyword in this instance

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7239
  • View blog
  • Posts: 24,538
  • Joined: 05-May 12

Re: Don't get the use of the "this" keyword in this instance

Posted 27 January 2020 - 06:11 PM

In that case, this is a reference to the current object. It's no different to the usage in your case #2 above where this is also a reference to the current object. It just happens that in the case #2, you are accessing fields and properties of the current object. In case #3, you aren't actually accessing anything, but rather simply passing the reference.

Remind us again which book or tutorial you are using to learn C#? Getting a solid grasp of references is important in learning C#. Unlike learning C, where learning the syntax is very important, in C# understanding object oriented concepts trumps learning the syntax.
Was This Post Helpful? 0
  • +
  • -

#3 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 22-February 17

Re: Don't get the use of the "this" keyword in this instance

Posted 28 January 2020 - 06:37 AM

Skydiver

I can see the use of this with 2 in that one wishes to be specific as to which variable you are referring to. Here a and b, where these have been initialised to 1 and 2 respectively.

However, I do not know what the point is of the this by itself as in 3. Maybe an easy example to illustrate? I have Googled this but the examples I find cover 1 and 2. Maybe not looking at the right place.

I do not use books, but rather Youtube videos covering specific topics. Did a basic beginners course initially.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3069
  • View blog
  • Posts: 11,770
  • Joined: 03-December 12

Re: Don't get the use of the "this" keyword in this instance

Posted 28 January 2020 - 07:19 AM

Quote

3. plusCommand = new PlusCommand(this)


It is called from another class. It passes an instance of the current class to the newly instantiated class.

Example: https://dotnetfiddle.net/oa51m2


using System;
public class Foo
{
	public Foo(object item)
	{
		Console.WriteLine(item);	
	}
}


public class Program
{
	public void bar()
	{
		var foo = new Foo(this);
	}
	
	public static void Main()
	{
		var p = new Program();
		p.bar();
	}
}



Was This Post Helpful? 1
  • +
  • -

#5 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 22-February 17

Re: Don't get the use of the "this" keyword in this instance

Posted 28 January 2020 - 12:10 PM

Ok, so my understanding is that

var foo = new Foo(this);



declares an object foo of type var and is a new instance of class Foo. So what specifically is the Foo(this) telling me?

I mean is,

var foo = new Foo(this);



simply telling me that the class Foo is outside the current class (Program)? Whereas,

var foo = new Foo();



is telling me that Foo is within the existing class?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1446
  • View blog
  • Posts: 4,289
  • Joined: 12-May 09

Re: Don't get the use of the "this" keyword in this instance

Posted 28 January 2020 - 12:19 PM

You're passing a reference to whatever the enclosing object to Foo is. So, if you did something like:

var thing = Thing("hi");
var thingHolder = Holder(thing);



You could actually make a Holder inside a Thing, like:

public Thing(string message) {
    var myHolder = Holder(this);
}

// as long as Holder does something like:
public Holder(Thing thing) {
}



this is just a way to get a reference to whatever object you're currently in.
Was This Post Helpful? 2
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3069
  • View blog
  • Posts: 11,770
  • Joined: 03-December 12

Re: Don't get the use of the "this" keyword in this instance

Posted 28 January 2020 - 12:25 PM

You are passing the object to other methods for use. https://docs.microso...e/keywords/this
Was This Post Helpful? 1
  • +
  • -

#8 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 22-February 17

Re: Don't get the use of the "this" keyword in this instance

Posted 28 January 2020 - 04:03 PM

I am close... the penny has not quite dropped yet, but I am close.

xclite - appreciate the effort.

You're passing a reference to whatever the enclosing object to Foo is.

So I ask myself what does he mean by enclosing object? Is that the class Program (as that is where Foo is now being used)?

Am I passing a reference to Program with the reference pointing to Foo? Is that correct?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1446
  • View blog
  • Posts: 4,289
  • Joined: 12-May 09

Re: Don't get the use of the "this" keyword in this instance

Posted 28 January 2020 - 04:11 PM

Right, so "this" is simply that - you're in an object, "always". In astonecipher's example, "this" refers to the instance of the Program class, yes.

However, you're not sending Program a reference. You're sending Foo a reference. That reference is pointing at a Program. You're basically saying "here, I'm making a Foo, and in that Foo I'm putting an address to myself (Program)".
Was This Post Helpful? 1
  • +
  • -

#10 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 22-February 17

Re: Don't get the use of the "this" keyword in this instance

Posted 29 January 2020 - 02:30 AM

1. So is my understanding of the following correct? (I've inserted in brackets the relevant class in astonecipher's statement):

It (Foo) is called from another class (Program). It (Foo) passes an instance of the current class (Foo) to the newly instantiated class (foo).

2. Then:

...You're sending Foo a reference. That reference is...

Should it not read:
...You're sending the instantiated Foo a reference. That reference is...?

3. I'm a bit confused here:

Right, so "this" is simply that - you're in an object, "always". In astonecipher's example, "this" refers to the instance of the Program class, yes.

However, you're not sending Program a reference. You're sending Foo a reference. That reference is pointing at a Program. You're basically saying "here, I'm making a Foo, and in that Foo I'm putting an address to myself (Program)".


Earlier you also said:

You're passing a reference to whatever the enclosing object to Foo is.


So if the enclosing object to Foo is Program, then you're sending Program a reference. Yet, above you say you're sending Foo a reference?

My apologies of I am misunderstanding here.


4. So does this mean whenever a class is instantiated in another class, that the this keyword needs to be used?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#11 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 990
  • View blog
  • Posts: 3,887
  • Joined: 13-June 14

Re: Don't get the use of the "this" keyword in this instance

Posted 29 January 2020 - 04:45 AM

Re: point 2: no. You're passing a reference to Foo's constructor, so until the code in there had executed, the object hasn't been instantiated.

4. Again no. If the class you're instantiating needs a reference to the class that's instantiating it, you'd pass this, otherwise not.

Do you understand passing references to a class' constructor or as method arguments in general? If so and you understand what this is a reference to, you should be able to join the dots.

As an example of where you might see such a thing, consider creating a tree structure. You might have a node that has a child node. To be able to navigate back up the tree, it would be useful for the child to have a reference to its parent. Hence, when you're in the parent, you'd pass this to the child.
Was This Post Helpful? 2
  • +
  • -

#12 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3069
  • View blog
  • Posts: 11,770
  • Joined: 03-December 12

Re: Don't get the use of the "this" keyword in this instance

Posted 29 January 2020 - 06:00 AM

Have you read over the link I posted? It gives examples as well for when you may want to use it.
Was This Post Helpful? 0
  • +
  • -

#13 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 22-February 17

Re: Don't get the use of the "this" keyword in this instance

Posted 31 January 2020 - 07:44 AM

astonecipher - yes I did thank you. Did it help? Not sure. Will read and study again.

ndc85430 - ok, putting this altogether: Let's suppose I run the Foo program of astonecipher:

The attached is what (I think) I will end up with. Is it correct?

After
var p = new Program();


has executed:

Address__Stack
100_______p
108_______pointer (Heap start address where members of p will reside)

After
p.bar();

has executed:

Address___Stack
100_______p
108_______pointer (Heap start address where members of p will reside)
116_______foo
124_______pointer (Heap start address where members of foo will reside)
132_______address 100 (pointer to p)

Is the above correct?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7239
  • View blog
  • Posts: 24,538
  • Joined: 05-May 12

Re: Don't get the use of the "this" keyword in this instance

Posted 31 January 2020 - 10:23 PM

Unfortunately no. Your and foo are already the pointers to the heap.
Was This Post Helpful? 0
  • +
  • -

#15 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 990
  • View blog
  • Posts: 3,887
  • Joined: 13-June 14

Re: Don't get the use of the "this" keyword in this instance

Posted 01 February 2020 - 01:37 AM

At this point, I'd be more concerned with understanding the high level idea of references, being able to pass them around, etc. than the low level details, as you seem to be struggling with the former.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2