Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:46:59+00:00 2026-06-05T20:46:59+00:00

im trying to learn delegates and events in c#, i understand that an event

  • 0

im trying to learn delegates and events in c#, i understand that an event is some sort of a wrapper for a delegate and a delegate is a pointer for functions/methods…

below is my code but when i run it, nothing is being shown… what could be the problems?

public class ClassHandler
{
    public delegate void DoProcesses();
    public event DoProcesses DoProcessesEvent;
}

public class Class1
{
    public void Func1()
    {
        Console.WriteLine("Class 1 doing function 1");
    }
    public void Func2()
    {
        Console.WriteLine("Class 1 doing function 2");
    }

}

public class Class2
{
    public void Func1()
    {
        Console.WriteLine("Class 2 doing function 1");
    }
    public void Func2()
    {
        Console.WriteLine("Class 2 doing function 2");
    }

}


class Program
{
    static void Main(string[] args)
    {
        Class1 cs1 = new Class1();
        Class2 cs2 = new Class2();
        ClassHandler main = new ClassHandler();
        main.DoProcessesEvent += new ClassHandler.DoProcesses(cs1.Func1);
        main.DoProcessesEvent += new ClassHandler.DoProcesses(cs1.Func2);
        main.DoProcessesEvent += new ClassHandler.DoProcesses(cs2.Func1);
        main.DoProcessesEvent += new ClassHandler.DoProcesses(cs2.Func2);
        main.DoProcessesEvent += new ClassHandler.DoProcesses(ff); // this line here is causing an error: An object reference is required for the non-static field, method, or property 'TryDelegatesAndEvents.Program.ff()'    

        Console.Read();
    }
    public void ff()
    {
        Console.WriteLine("gggg");
    }
}

UPDATE: how do i raise the event so it will execute the methods already?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:47:01+00:00Added an answer on June 5, 2026 at 8:47 pm

    Problem with this line: main.DoProcessesEvent += new ClassHandler.DoProcesses(ff)

    That is because your method ff() is a non-static method and you can’t access it directly like that from a static method.

    Make your method ff as static, or create and object of the containing class and assign the method with an instance of it.

    For Comments: The reason you are not seeing anything is because you are just binding them to an event DoProcessesEvent, but you are not raising the event any where. You are only defining the handler for the event.

    EDIT:
    Change your ClassHandler class to:

    public class ClassHandler
    {
        public delegate void DoProcesses();
        public event DoProcesses DoProcessesEvent;
    
        public void OnDoProcessEvent()
        {
            if (DoProcessesEvent != null)
                DoProcessesEvent();
        }
    }
    

    In your Main method before Console.Read(); Type:

    main.OnDoProcessEvent();
    

    This will raise the event and it will handled from the application and will give you the following output.

    Class 1 doing function 1
    Class 1 doing function 2
    Class 2 doing function 1
    Class 2 doing function 2
    gggg
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to learn about events/delegates, but am confused about the relationship between
I am trying to learn on how to use delegates efficiently in C# and
Ive been playing around with delegates trying to learn and I ran into one
I'm trying to learn delegate and not sure what I do wrong. So I
Im trying to learn a bit about c++ and have run in to some
I'm trying to learn more about WPF. I ran through an online tutorial that
I am trying to learn some c# coding and wondering if the c++ concept
Trying to learn PL/SQL with multimedia data. Can anyone please point out that from
I've been trying to learn delegates.I just created a button,label and checkbox. If I
I am trying to learn CorePlot. I found a code example from codejunkster that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.