Sign in to follow this  

Passing this as a ref.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
you mean using a ref in a params like:

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

or something like this?

[note: this is also invalid]
[CODE]
class Foo{

static Main()
{
Foo.bar(ref this);
}

public static void bar(ref Foo foo)
{
//do something
}
}
[/CODE] Edited by Net Gnome

Share this post


Link to post
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 this post


Link to post
Share on other sites
This will work as a round-about:

[CODE]
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());
}
}
[/CODE] Edited by Net Gnome

Share this post


Link to post
Share on other sites
[quote name='Net Gnome' timestamp='1343303140' post='4963263']
This will work as a round-about:

[CODE]
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());
}
}
[/CODE]
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='kunos' timestamp='1343306480' post='4963273']
and what would be the point to pass a ref to this in a reference type again?
[/quote]

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
[CODE]
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);
}
}[/CODE]

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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:
[CODE]
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);
}[/CODE]

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 this post


Link to post
Share on other sites
[quote name='Net Gnome' timestamp='1343319227' post='4963351']
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:
[CODE]
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);
}[/CODE]

output:
1:1:1
2:2:2
yes
2:2:2
3:3:3
yes
1:0
no
1:1
yes
[/quote]

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 this post


Link to post
Share on other sites
[quote name='Arthur Souza' timestamp='1343329118' post='4963401']
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.
[/quote]

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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this