e.g. something like ( ref this ) which does not work ... E.g. this fails:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CopyOfThis
{
class Program
{
static void Main(string[] args)
{
View objView = new View();
objView.Boo();
objView.ShowMsg("The objView.StrVal is " + objView.StrVal);
Console.Read();
}
} //eof Program
class View
{
private string strVal;
public string StrVal
{
get { return strVal; }
set { strVal = value; }
}
public void Boo()
{
Controller objController = new Controller(ref this);
}
public void ShowMsg ( string msg )
{
Console.WriteLine(msg);
}
} //eof class
class Controller
{
View View { get; set; }
public Controller(View objView)
{
this.View = objView;
this.LoadData();
}
public void LoadData()
{
this.View.StrVal = "newData";
this.View.ShowMsg("the loaded data is" + this.View.StrVal);
}
} //eof class
class Model
{
} //eof class
} //eof namespace
this
is already a reference. Code like
DoSomethingWith(this);
is passing the reference to the current object to the method DoSomethingWith
.
EDIT: Given your edited code example, you don't need to pass ref this
since as the accepted answer states - this is already a reference.
you can't pass the this
reference by reference because it is constant; passing it by ref would allow it to be changed - which is nonsense as far as C# is concerned.
The closest you can get is to do:
var this2 = this;
foo(ref this2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With