# Passing this as a ref.

This topic is 2038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well this post is much more clearer than my last help post I hope. Basically is there any workaround to pass ref this as a parameter. To a class members function.

##### Share on other sites
you mean using a ref in a params like:

[note: this is invalid i believe]
 public void foo(params ref Bar[]) { //do stuff here } 

or something like this?

[note: this is also invalid]
 class Foo{ static Main() { Foo.bar(ref this); } public static void bar(ref Foo foo) { //do something } }  Edited by Net Gnome

##### Share on other sites
what are you trying to accomplish by passing this as reference? It can't imagine a situation where you'd want that... unless you are talking about value types (structs)

##### Share on other sites
This will work as a round-about:

 class Foo { private Foo thisFoo; public Foo() { thisFoo = this; } public static void Main() { Foo foo = new Foo(); foo.doSomething(); Console.ReadLine(); } public void doSomething() { bar(ref thisFoo); } public void bar(ref Foo foo) { Console.WriteLine(foo.ToString()); } }  Edited by Net Gnome

##### Share on other sites
Thank you Net Gnome your a life saver man. And thanks everybody for taking the time to read this. Edited by Thekill473

##### Share on other sites

This will work as a round-about:

 class Foo { private Foo thisFoo; public Foo() { thisFoo = this; } public static void Main() { Foo foo = new Foo(); foo.doSomething(); Console.ReadLine(); } public void doSomething() { bar(ref thisFoo); } public void bar(ref Foo foo) { Console.WriteLine(foo.ToString()); } } 

and what would be the point to pass a ref to this in a reference type again?

##### Share on other sites
I second Kunos, I don't think that this will do what you think it is going to do...

##### Share on other sites
Well basically I just needed to be able to fiddle around with multiple things in the owner class. But net's solution worked perfectly.

##### Share on other sites

and what would be the point to pass a ref to this in a reference type again?

Irrelevant. The man asked a question. I gave him an answer. Why he needs it is irrelevant.

[s]If i had to take a WAG, he's using an API that requires a ref param to a type he's extended [/s][The OP responded]. You cant pass "this" as a refernece as that is illegal, nor did he have an object to pass due to the scope of the call, so he needed another way to pass the class, which was to create an object reference of the class within itself upon instantiation, thus creating an object that is usable by a ref parameter.

if you used this
 class Foo { public int Val = 0; private Foo thisFoo; public Foo() { thisFoo = this; } public static void Main() { Foo foo = new Foo(); foo.Val = 1; foo.doSomething(); Console.ReadLine(); } public void doSomething() { bar(ref thisFoo); } public void bar(ref Foo foo) { Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); foo.Val += 1; Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); } }

the output would be
1:1:1
2:2:2 Edited by Net Gnome

##### Share on other sites
I may be missing something obvious here, but I still don't see the need for "ref" here. The code would work as intended without ref, wouldn't it?

If the the OP is using or extending an API, then possibly it would make sense to me, although a bit odd.

For my part, I am unsure of the OP's famiarity with C#, so my first intuition was that there was some confusion about ref paramters vs. pass by reference. I know coming from C++ this was a point of confusion for me.

##### Share on other sites
you guys dont seem to understand that in C# "this" is ALREADY a reference... the answer posted by the OP (fiddling with things in the onwer class) clearly shows he doesn't understand that he can do that with a normal reference (not ref).. passing a ref to a reference is only useful in case the function wants to reassign that reference to something else.. something similar to what you do in C++ with a pointer to a pointer. And so it is clear why a ref to this is not allowed, if i pass a ref to "this" to a function and a function reseats the ref, what happen to me and the rest of the caller function?
The "solution" given, is not a solution, even if the function reseats the "fake" this, it will just point that to another class instance.

In other words, this doesn't make any sense at all, and it is a sign of total lack of understanding of the language in use.

##### Share on other sites
I -never- stated it was a solution. I stated it was a work-around to his problem. Also, there is no need to berate the man. His question was valid, (paraphrasing) "why wont this work". You're right in that using ref in this case doesnt solve anything due to the fact that the class construct is just a reference mechanism so you dont have to deal with pointers. using bar(Foo foo) is the same as using bar(ref Foo foo) if Foo is a class. However it is -not- the same if Foo is a struct.

demo'd by this program:
 struct Foo2 { public int Val; public void bar(Foo2 foo) { foo.Val += 1; Console.WriteLine(foo.Val + ":" + this.Val); if (object.Equals(foo, this)) Console.WriteLine("yes"); else Console.WriteLine("no"); } public void bar2(ref Foo2 foo) { foo.Val += 1; Console.WriteLine(foo.Val + ":" + this.Val); if (object.Equals(foo, this)) Console.WriteLine("yes"); else Console.WriteLine("no"); } } class Foo { public int Val = 0; private Foo thisFoo; public Foo() { thisFoo = this; } public static void Main() { Foo foo = new Foo(); foo.Val = 1; foo.doSomething(); foo.doSomethingElse(); Console.ReadLine(); } public void doSomething() { bar(ref thisFoo); bar2(thisFoo); } public void bar(ref Foo foo) { Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); foo.Val += 1; Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); if (foo == this && foo == thisFoo) Console.WriteLine("yes"); else Console.WriteLine("no"); } public void bar2(Foo foo) { Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); foo.Val += 1; Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); if (foo == this && foo == thisFoo) Console.WriteLine("yes"); else Console.WriteLine("no"); } public void doSomethingElse() { Foo2 foo = new Foo2(); foo.Val = 0; foo.bar(foo); foo.bar2(ref foo); }

output:
1:1:1
2:2:2
yes
2:2:2
3:3:3
yes
1:0
no
1:1
yes Edited by Net Gnome

##### Share on other sites

I -never- stated it was a solution. I stated it was a work-around to his problem. Also, there is no need to berate the man. His question was valid, (paraphrasing) "why wont this work". You're right in that using ref in this case doesnt solve anything due to the fact that the class construct is just a reference mechanism so you dont have to deal with pointers. using bar(Foo foo) is the same as using bar(ref Foo foo) if Foo is a class. However it is -not- the same if Foo is a struct.

demo'd by this program:
 struct Foo2 { public int Val; public void bar(Foo2 foo) { foo.Val += 1; Console.WriteLine(foo.Val + ":" + this.Val); if (object.Equals(foo, this)) Console.WriteLine("yes"); else Console.WriteLine("no"); } public void bar2(ref Foo2 foo) { foo.Val += 1; Console.WriteLine(foo.Val + ":" + this.Val); if (object.Equals(foo, this)) Console.WriteLine("yes"); else Console.WriteLine("no"); } } class Foo { public int Val = 0; private Foo thisFoo; public Foo() { thisFoo = this; } public static void Main() { Foo foo = new Foo(); foo.Val = 1; foo.doSomething(); foo.doSomethingElse(); Console.ReadLine(); } public void doSomething() { bar(ref thisFoo); bar2(thisFoo); } public void bar(ref Foo foo) { Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); foo.Val += 1; Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); if (foo == this && foo == thisFoo) Console.WriteLine("yes"); else Console.WriteLine("no"); } public void bar2(Foo foo) { Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); foo.Val += 1; Console.WriteLine(foo.Val + ":" + thisFoo.Val + ":" + this.Val); if (foo == this && foo == thisFoo) Console.WriteLine("yes"); else Console.WriteLine("no"); } public void doSomethingElse() { Foo2 foo = new Foo2(); foo.Val = 0; foo.bar(foo); foo.bar2(ref foo); }

output:
1:1:1
2:2:2
yes
2:2:2
3:3:3
yes
1:0
no
1:1
yes

So asking him about the reason for that question so that we can work on a better solution and explain him the issue is irrelevant? I don't think so.

##### Share on other sites

So asking him about the reason for that question so that we can work on a better solution and explain him the issue is irrelevant? I don't think so.

My appologies, I did get a bit mean myself there with the irrelevant comment. I just saw this quickly turning into another "why are you doing that?! are you stupid?" post, and I f'd up and fed the fire. I've been where this guy has been before and hated uppity communities not answering the question I asked, but instead looking for a question they wanted to answer. I fired back inappropriately and a bit unprofessionally, and for that I appologize. Edited by Net Gnome

##### Share on other sites
Aha I think I must've skipped a chapter in my book that said it automatically passes parameters as a ref. Thanks for the clear up guys.