tags:

views:

68

answers:

2

This question is related to c#.The scenario is that when i click the button the operations like File reading,Data manipulation ,and file dumping are going to be happened.After the completion of each operation i will update the status(i.e,File reading completed,data manipulation completed) in the label which is in UI(FORM-frmTesting)

The button click event is

namespace frmTesting
      {
         public partial class Form1 : Form
         {
              private void button1_Click_1(object sender, EventArgs e)
              {
                 class1 l_objClass1 = new class1();
                  l_objClass1.DoOperation();

              }
          }


       public class class1
       {

            public int DoOperation()
            {
                  ReadTextFile();
                  ParsingData();
                  SaveTextFile();
                  return 0;
            }

            private int ReadTextFile()
            {
               //Read the text File
                return 0;

            }

            private int ParsingData()
            {
               // Data manipulation
                return 0;

            }

            private int SaveTextFile()
            {

               // save the file
               return 0;
            }
       }
   }

Is it possible by using Delegates and Events....if you have any queries plz revert back me

A: 

The short answer is yes. You add events to class1 and add handlers to Form1 with the appropriate logic. Below is a sample of how to do this

public partial class Form1 : Form
    {


        private void button1_Click_1(object sender, EventArgs e)
        {
            class1 obj = new class1();
            obj.FileReadingComplete += HandleFileReadingComplete;
            obj.DataManipulationComplete += HandleDataManipulationComplete;

            obj.DoOperation();

            obj.FileReadingComplete -= HandleFileReadingComplete;
            obj.DataManipulationComplete -= HandleDataManipulationComplete;

        }

        private void HandleFileReadingComplete(object sender, EventArgs args){ 
            //code here
        }

        private void HandleDataManipulationComplete(object sender, EventArgs args)
        {
            //code here
        }

    }


    public class class1
    {

        public event EventHandler FileReadingComplete;
        public event EventHandler DataManipulationComplete;

        public int DoOperation()
        {
            ReadTextFile();
        OnFileReadingComplete();
        ParsingData();
        OnDataManipulationComplete();
        SaveTextFile();
        return 0;
        }

        private int ReadTextFile()
        {
            //Read the text File
            return 0;

        }

        private int ParsingData()
        {
            // Data manipulation
            return 0;

        }

        private int SaveTextFile()
        {

            // save the file
            return 0;
        }

        public void OnFileReadingComplete()
        {
            EventHandler handler = FileReadingComplete;

            if (handler != null) {
                handler(this, EventArgs.Empty);
            }
        }

        public void OnDataManipulationComplete()
        {
            EventHandler handler = DataManipulationComplete;

            if (handler != null) {
                handler(this, EventArgs.Empty);
            }
        }
    }
Craig Suchanec
+1  A: 

You'll have to modify class1 to broadcast events that other classes can listen to:

public class class1
{
    // Not necessary, but will allow you to add custom EventArgs later
    public delegate void StatusChangedEventHandler(object sender, EventArgs e);

    public event StatusChangedEventHandler FileRead;
    public event StatusChangedEventHandler FileParsed;
    public event StatusChangedEventHandler FileSaved;

    public int DoOperation()    
    {    
        ReadTextFile();    
        ParsingData();    
        SaveTextFile();    
        return 0;    
    }    

    private int ReadTextFile()    
    {    
        //Read the text File
        OnFileRead(EventArgs.Empty);   
        return 0;
    }    

    private int ParsingData()    
    {    
        // Data manipulation
        OnFileParsed(EventArgs.Empty);
        return 0;       
    }    

    private int SaveTextFile()    
    {      
        // save the file
        OnFileSaved(EventArgs.Empty);
        return 0;    
    }

    protected virtual void OnFileRead(EventArgs e)
    {
        if(FileRead != null)
            FileRead(this, e);
    }

    protected virtual void OnFileParsed(EventArgs e)
    {
        if(FileParsed != null)
            FileParsed(this, e);
    }

    protected virtual void OnFileSaved(EventArgs e)
    {
        if(FileSaved != null)
            FileSaved(this, e);
    }
}

And then have your form listen for those events and change its label appropriately:

public partial class Form1 : Form 
{ 
    private void button1_Click_1(object sender, EventArgs e) 
    { 
        class1 l_objClass1 = new class1();

        l_objClass1.FileRead += 
            delegate { lblStatus.Text = "File Read..."; };

        l_objClass1.FileParsed += 
            delegate { lblStatus.Text = "File Parsed..."; };

        l_objClass1.FileSaved += 
            delegate { lblStatus.Text = "File Saved..."; };

        l_objClass1.DoOperation(); 
    } 
} 
Justin Niessner
hi,justin.....can you tell me the sequence call to be used in button_click event
@user374191 - I'm not sure exactly what you mean.
Justin Niessner
FileRead(EventArgs.Empty); -----how tis s possible....it takes only one argument
@user374191 - Sorry. I forgot the On before the handler names.
Justin Niessner
can u able to edit it now? FileRead(this, EventArgs.Empty);