[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simple delegates question

Posted on 2005-04-09
1
Medium Priority
?
257 Views
Last Modified: 2012-05-05
Hi, I am trying to understand delegates a little better as a c# beginner.  Why does the call to getDel not cause execution of both getMe and getX.  I do not think the abstract class has any relevance to this question, I just do not know why both functions - getX and getMe - are not executed as they are both registered.  Only getX is executed.

Thanks for explaining more about delegates - but please do not post a bare link to a lot of text on delegates, I just want it explained on here concisely ;)

using System;

namespace Abstract1
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  Person p = new Person();
                  p.getDel();
            }
      }
}


using System;

namespace Abstract1
{
      /// <summary>
      /// Summary description for Being.
      /// </summary>
      ///
      public delegate int NDelegate();

      public abstract class Being
      {
            private NDelegate adel;

            public Being()
            {
                  adel = new NDelegate(getMe);
                  adel += new NDelegate(getX);
            }

            public void getDel()
            {
                  Console.WriteLine("{0}", adel());
            }

            public abstract int getMe();
            public abstract int getX();
            public abstract int setMe(int i);

            public int anotherMe()
            {
                  return 50;
            }
      }

      public class Person : Being
      {
            public override int getMe()
            {
                  return 55;
            }

            public override int getX()
            {
                  return 56;
            }

            public override int setMe(int i)
            {
                  return i * 3;
            }


      }
}
0
Comment
Question by:jasoncpp
1 Comment
 
LVL 10

Accepted Solution

by:
eternal_21 earned 2000 total points
ID: 13744402
Both functions are executed, which you can see by modifying them as follows,

    public override int getMe()
    {
      Console.WriteLine("getMe Invoked");
      return 55;
    }

    public override int getX()
    {
      Console.WriteLine("getX Invoked");
      return 56;
    }

The reason that your call, "adel()" only returns 56, is because you can only access the last value returned by the list of functions invoked.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question