Reference to a Reference?

Started by
5 comments, last by Ronnie Mado Solbakken 10 years, 8 months ago

Is this possible without venturing into unmanned code land? Would this hypothetical method still work If I wanted a reference to a reference of a delegate instead of cows?


namespace ContrivedExample
{
    class cow
    {
        int Weight;

        public cow(int i) { Weight = i; }

        public void HowMuchWeight()
        {
            Console.WriteLine(Weight);
        }
    }
    public class program
    {
        static void Main()
        {
            cow RefCow;
            cow Cow1 = new cow(5);
            cow Cow2 = new cow(10);
            cow ReftoRefCow;

            RefCow = Cow1;           
            ReftoRefCow = RefCow;
            ReftoRefCow.HowMuchWeight(); // Prints 5 as it should

            RefCow = Cow2;
            ReftoRefCow.HowMuchWeight(); //Still prints 5. I want to somehow make it say 10
        }
    }
}
Advertisement
You have to build a new type to do the same thing explicitly:


class RefToCow
{
  public cow cow;
}

...

static void Main()
{
  RefToCow RefCow;
  cow Cow1 = new cow(5);
  cow Cow2 = new cow(10);
  RefToCow RefToRefCow;

  RefCow.cow = Cow1;
  RefToRefCow = RefCow;
  ReftoRefCow.cow.HowMuchWeight();

  RefCow.cow = Cow2;
  ReftoRefCow.cow.HowMuchWeight(); // Prints 10 now.
}
This has the same behavior as doing pointer-to-pointer in C and C++. You just have to spell it all out using a member variable.

Ok, thanks, that worked with delegates. However not the event syntax. I had to expose a delegate reference object for each event I have. And basically the event are going unused. I'd like to be able to prevent my delegates from being nulled or invoked from the out side.

That sounds kind of strange to me. Can you post an example of what you are trying to do?

The code is way too big to post here.

I am using events for inter-object communication, but the objects don't directly know about each other so it's impossible to directly subscribe to one another's events directly. What I do instead is give groups of related objects a pool of events that they can each dump their events into and can look through and pick out ones they are interested in to subscribe to. It's in this pool that I need to keep a reference to the events. This was my initial intention at least, until I had difficulty referencing events.

Now I can't make your suggestion work for actual events so instead I've made each object give that pool an "Message" object that stores a delegate.

Is this possible without venturing into unmanned code land?

In general, the C# idiom is to make RefToRefCow a Func<RefCow>


var RefCow = Cow1;
var RefToRefCow = ()=>RefCow;
RefToRefCow().HowMuchWeight();  // 5

RefCow = Cow2;
RefToRefCow().HowMuchWeight(); // 10

Why would you ever want to reference a reference? Why not just refer directly? I'm just curious, as I'm new to coding. I can understand it if you're trying to reference an instance rather than its parent object, though.

- Awl you're base are belong me! -

- I don't know, I'm just a noob -

This topic is closed to new replies.

Advertisement