Passing this as a ref.

Started by
13 comments, last by Thekill473 11 years, 8 months ago
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.
Advertisement
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
}
}
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)

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

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());
}
}
Thank you Net Gnome your a life saver man. And thanks everybody for taking the time to read this.

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?

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

I second Kunos, I don't think that this will do what you think it is going to do...
Well basically I just needed to be able to fiddle around with multiple things in the owner class. But net's solution worked perfectly.

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
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.

This topic is closed to new replies.

Advertisement