Sign in to follow this  
Grain

Reference to a Reference?

Recommended Posts

Grain    500

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
        }
    }
}

Share this post


Link to post
Share on other sites
Nypyren    12063
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. Edited by Nypyren

Share this post


Link to post
Share on other sites
Grain    500

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.

Share this post


Link to post
Share on other sites
Grain    500

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. 

Share this post


Link to post
Share on other sites
Telastyn    3777
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

Share this post


Link to post
Share on other sites

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.

Edited by Malabyte

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this