none
C# - Access to private method from other class RRS feed

  • Question

  • Hello,

    In 'Worker' class, 'del' variable eventually access to private method in 'Program' class ( send parameter to Worker_WorkPerformed method in Program class but Worker_WorkPerformed is private). but According to the contract, private method can only access from own class. Not others.

    How to access private method? Thanks

    using System;
    
    
    namespace DelegatesAndEvents
    {
        public class WorkPerformedEventsArgs : EventArgs
        {
            public WorkPerformedEventsArgs(int hours , WorkType workType)
            {
                Hours = hours;
                WorkType = workType;
            }
            public int Hours { get; set; }
            public WorkType WorkType { set; get; }
        }
    }


    using System;
    
    
    namespace DelegatesAndEvents
    {
        //public delegate int WorkdPerformedHandler(object o , WorkPerformedEventsArgs e);
    
    
        public class Worker
        {
            public event EventHandler<WorkPerformedEventsArgs> WorkPerformed;
            public event EventHandler WorkCompleted;
    
    
            public void DoWork(int hours , WorkType workType)
            {
                for (int i = 0; i < hours; i++)
                {
                    OnWorkPerfomed(i + 1, WorkType.Coffee);
                }
                OnWorkCompleted();
            }
            protected virtual void OnWorkPerfomed(int hours , WorkType workType)
            {
                var del = WorkPerformed as EventHandler<WorkPerformedEventsArgs>;
                if (del != null)
                {
                    del(this, new WorkPerformedEventsArgs(hours,workType));
                }
            }
            protected virtual void OnWorkCompleted()
            {
                var del = WorkCompleted as EventHandler;
                if (del != null)
                {
                    del(this, EventArgs.Empty);
                }
            }
        }
    }


    using System;
    
    
    namespace DelegatesAndEvents
    {
        public enum WorkType
        {
            GoToMeeting,
            Coffee,
            Break
        }
        class Program 
        {
            static void Main(string[] args)
            {
                Worker worker = new Worker();
                worker.WorkPerformed += Worker_WorkPerformed;
                
                worker.DoWork(10, WorkType.GoToMeeting);
            }
    
    
            private static void Worker_WorkPerformed(object sender, WorkPerformedEventsArgs e)
            {
                Console.WriteLine($"Hour is {e.Hours} - WOrk Type is {e.WorkType}");
            }
        }
    }

    Worker Class:

    Sunday, April 1, 2018 11:37 AM

All replies

  • How to access private method? Thanks

    By exposing a public method in the class that access the private method in the class is how you can do it.

    Sunday, April 1, 2018 1:04 PM