Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to use delegates as properties in C#?

I need to create a class with two properties:

  1. LogOutput
  2. ExceptionOutput

These properties (Actions<>) send a message or a exception depending on the target function. This target function is set via properties.

Currently, I have this functional code:

    public class Output
    {
        private Action<string> logOutput;
        private Action<Exception, string> exceptionOutput;

        public Action<string> LogOutput { set { this.logOutput = value; } get { return this.logOutput; } }
        public Action<Exception, string> ExceptionOutput { set { this.exceptionOutput = value; } get { return this.exceptionOutput; } }

        public Output() : this(null, null) { }

        public Output(Action<string> logAction, Action<Exception, string> exceptionAction) 
        {
            this.logOutput = logAction;
            this.exceptionOutput = exceptionAction;
        }


        public void WriteLogMessage(string format, params object[] args) 
        {
            if (this.logOutput != null)
                logOutput(string.Format(format, args));
        }

        public void WriteExceptionMessage(Exception ex, string format, params object[] args) 
        {
            if (this.exceptionOutput != null)
                exceptionOutput(ex, string.Format(format, args));
        }
    }

And this is my form code:

    private void MainForm_Load(object sender, EventArgs e)
    {
        // my Output object
        Output myOutput = new Output();

        // set properties
        myOutput.ExceptionOutput = this.WriteExceptionMessageToTextBox;
        myOutput.LogOutput = this.WriteLogMessageToTextBox;

        // test
        myOutput.WriteLogMessage("this is my log message to text box");
        myOutput.WriteExceptionMessage(new Exception("this is my exception"), "this is my exception message to text box");
    }

    private void WriteLogMessageToTextBox(string message)
    {
        // nothing to do here
        if (this.txtBox.IsDisposed)
            return;

        if (this.InvokeRequired)
        {
            BeginInvoke(new MethodInvoker(delegate() { WriteLogMessageToTextBox(message); }));
        }
        else 
        {
            // write to text box
            this.txtBox.AppendText(message + Environment.NewLine);
        }
    }

    private void WriteExceptionMessageToTextBox(Exception ex, string message)
    {
        // nothing to do here
        if (this.txtBox.IsDisposed)
            return;

        if (this.InvokeRequired)
        {
            BeginInvoke(new MethodInvoker(delegate() { WriteExceptionMessageToTextBox(ex, message); }));
        }
        else
        {
            string msg = "";
            msg += string.Format("Program:{0}", message);
            msg += string.Format("Message{0}", ex.Message);
            msg += string.Format("StackTrace:{0}", ex.StackTrace);
            msg += string.Format("Source:{0}", ex.Source);

            // write to text box
            this.txtBox.AppendText(msg + Environment.NewLine);
        }
    }

It is correct this model? There is another way to do this?

like image 524
auraham Avatar asked Jan 16 '23 04:01

auraham


2 Answers

It is correct this model? There is another way to do this?

There is nothing wrong with this, necessarily. However, events may be a more common approach to handling this, as you're effectively using the delegate as an event in this scenario.

Using events does have one significant advantage (potentially), in that you can also easily have multiple subscribers, which would make it simple to allow more than one item to "listen" to the exceptions or log messages. (*While this works with delegates as well, it wouldn't be as standard of a way to use delegates..)

like image 191
Reed Copsey Avatar answered Jan 28 '23 13:01

Reed Copsey


sorry offtopic but use StringBuilder string is not prefer for editing

            string msg = "";
            msg += string.Format("Program:{0}", message);
            msg += string.Format("Message{0}", ex.Message);
            msg += string.Format("StackTrace:{0}", ex.StackTrace);
            msg += string.Format("Source:{0}", ex.Source);


            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("Program:{0}", message));
            sb.Append(string.Format("Message{0}", ex.Message));
            sb.Append(string.Format("StackTrace:{0}", ex.StackTrace));
            sb.Append(string.Format("Source:{0}", ex.Source));
            string result = sb.ToString();
like image 34
burning_LEGION Avatar answered Jan 28 '23 13:01

burning_LEGION