Jump to content
  • Advertisement
Sign in to follow this  
Thekill473

Passing this as a ref.

This topic is 2157 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 ref this as a parameter. To a class members function.

Share this post


Link to post
Share on other sites
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
}
}
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:


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


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


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


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


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!