Pass data from class to Form through different class

208 views Asked by At

Is there a better solution to send data from one class to another through different class? I have image in class ImageProccessing and I would like to send him to class MainForm.

  • MainForm.cs (GUI)
  • VideoProccessing.cs
  • ImageProccessing.cs

I have this pseudocode:

    class MainForm : Form
    {
        VideoProccessing _video = new VideoProccessing();
    }

    class VideoProccessing
    {
        ImageProccessing _image = new ImageProccessing();
    }

    class ImageProccessing
    {
        Bitmap _bmp = null;

        public void SomeBadassProcess()
        {
            _bmp = new Bitmap();          //How to send this to MainForm (GUI)
        }
    }

My solution:

    class MainForm : Form
    {
        VideoProccessing _video = new VideoProccessing();
        _video.SendAgain += (ShowImage);

        private void ShowImage(Bitmap bmp)
        {
            SomePictureBox.Image = bmp;
        }

    }

    class VideoProccessing
    {
        ImageProccessing _image = new ImageProccessing();
        _image.Send += (ReceivedImage)

        public delegate void SendAgainImage(Bitmap bmp);
        public event SendAgainImage SendAgain;

        private void ReceivedImage(Bitmap bmp)
        {
            SendAgain(bmp);
        }
    }

    class ImageProccessing
    {
        Bitmap _bmp = null;

        public delegate void SendImage(Bitmap bmp);
        public event SendImage Send;

        public void SomeBadassProcess()
        {
            _bmp = new Bitmap();          //How to send this to MainForm (GUI)

            Send(_bmp);
        }
    }
1

There are 1 answers

0
AudioBubble On BEST ANSWER

If your VideoProcessing class has a dependency on the ImageProcessing class, however, you only wish to make your MainForm aware of VideoProcessing, then propagating your message in the manner that you are is probably acceptable. It does however create a strong coupling between your objects, which, you may or may not care about at this stage.

Another method could be to use some sort of message bus such as an EventAggregator or a Mediator. Both of these behavioural patterns are designed to decouple objects involved in the messaging process and provide a publish/subscribe mechanism.

As an example if you were to implement the EventAggregator your MainForm would be a subscriber, listening out for notifications sent from a publisher which in your scenario is the ImageProcessing class.

A contrived code example might look like the following:

public class MainForm : Form
{
    private readonly EventAggregator eventAggregator;
    private readonly VideoProcessing videoProcessing;

    public MainForm()
    {
        this.eventAggregator = new EventAggregator();
        this.eventAggregator.Subscribe<NewBitmapEvent>(HandleNewBitMap);
        this.videoProcessing = new VideoProcessing(this.eventAggregator);
    }

    private void HandleNewBitmap(Bitmap args)
    {
        // Do something
    }
}

public class VideoProcessing
{
    private readonly EventAggregator eventAggregator;

    private readonly ImageProcessing imageProcessing;

    public VideoProcessing(EventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        this.imageProcessing = new ImageProcessing(this.eventAggregator);
    }
}


public class ImageProcessing
{
    private readonly EventAggregator eventAggregator;

    public ImageProcessing(EventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        this.eventAggregator.Publish<NewBitmapEvent>(new Bitmap(...));
    }
}

When the ImageProcessing class publishes a NewBitmapEvent, only the MainForm is notified as only it, and not the VideoProcessing class is subscribed to the event. Obviously if other classes were subscribed then they too would be notified when the event is published.