How to pass the link of the current instance in C #

eg. something like (ref this) that doesn't work ... for example. 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 
+6
reference c #
source share
4 answers

this already a link. Code for example

 DoSomethingWith(this); 

passes a reference to the current object to the DoSomethingWith method.

+14
source share

EDIT: Given your edited sample code, you do not need to pass ref this , because since the accepted state of the response is already a link.

You cannot pass the this link to the link because it is permanent; passing it ref, it could be changed - this is nonsense regarding C #.

Closest you can do:

 var this2 = this; foo(ref this2); 
+4
source share

Remember that a variable passed as ref must be initialized earlier.

0
source share

If you have a way like this:

 void Foo(ref Bar value) { // bla bla } 

You can call it from the Bar object by creating a temporary variable like this

 var temp = this; foo.Foo(ref temp); 
0
source share

All Articles