Jump to content

  • Log In with Google      Sign In   
  • Create Account

Reference to a Reference?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Grain   Members   -  Reputation: 447

Like
0Likes
Like

Posted 16 August 2013 - 09:52 PM

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


Sponsor:

#2 Nypyren   Crossbones+   -  Reputation: 4279

Like
1Likes
Like

Posted 16 August 2013 - 10:23 PM

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, 16 August 2013 - 10:32 PM.


#3 Grain   Members   -  Reputation: 447

Like
0Likes
Like

Posted 17 August 2013 - 12:10 AM

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.



#4 Nypyren   Crossbones+   -  Reputation: 4279

Like
0Likes
Like

Posted 17 August 2013 - 03:38 AM

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

#5 Grain   Members   -  Reputation: 447

Like
0Likes
Like

Posted 17 August 2013 - 04:07 AM

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. 



#6 Telastyn   Crossbones+   -  Reputation: 3726

Like
3Likes
Like

Posted 17 August 2013 - 07:38 AM

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


#7 Malabyte   Members   -  Reputation: 589

Like
0Likes
Like

Posted 17 August 2013 - 09:30 AM

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, 17 August 2013 - 09:32 AM.

- Awl you're base are belong me! -

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS