tags:

views:

265

answers:

6

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 
+7  A: 

this is already a reference. Code like

DoSomethingWith(this);

is passing the reference to the current object to the method DoSomethingWith.

Peter Lillevold
I *think* the OP might be wanting to pass a copy of the reference, hence the "(ref this)" bit of the quesiton. Could be wrong though, as not really clear.
Rob Levine
And of course, if the method is on the same object you don't need to pass it anyway, just use this in the method.
jbloomer
Jupp. I will revise accordingly when OP provides us with more background.
Peter Lillevold
A: 

Remember that the variable passed as ref should be initialized before.

Andres
A: 

You can't change this reference.

Dmitry Borovsky
+1  A: 

I think I follow you, so I'll do my best to answer.

this is already a reference to the current instance, so if you want to pass around a reference to the current instance you can do the following:

myClass.MyMethod(this);

However, if you wanted to pass around a copy of the "this" reference (rather than the instance it is referencing), you would need to copy it to another reference first:

var copyOfThis = this;
myClass.MyMethod(ref copyOfThis);

This is because you can't actually pass this directly as a ref. this always points to the current instance, so it makes no sense to be able to pass around the reference itself.

Rob Levine
+3  A: 

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);
Andras Zoltan
A: 

If you have a method like this:

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

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

var temp = this;
foo.Foo(ref temp);
GvS