3

I need help with the following design. Basically I have a Main form which initializes a class called Settings. When the user clicks on a form called CommunicationsSetupForm, Main passes the Settings class in its ctor. From this CommunicationsSetupForm, the user can change settings such as the type of communication (RS232, RS485, TCP/IP, etc), baud rates, COM port names, etc.

Once this is done, focus is back to the Main form. From here, the user can send a packet to the respective hardware, such as a RUN packet. The packet is initalized through the Init Packet class, which also takes in the Settings class in its CTOR, than InitalizePacket calls PacketGenerate, which also passes the Settings class in its CTOR, and than finally CommunicationMediator is called via an event from PacketGenerate. CommunicationMediator is actually initialized in the Main Form, and also takes in the Settings class via its CTOR. Below is a picture of what is going on and I hope it makes the situation a little more clear: enter image description here

My question is, what is the best way for CommunicationMediator to know if the Settings class has changed. For example, the user now has changed the baud rate to 300 from 9600. From my research, I have read about "Deep Copy" and a way to implement it: How do you do a deep copy of an object in .NET (C# specifically)?. Using this technique, I can create a non-referenced copy of the class called OldSettings and compare it with the current Settings class each time I need to send something out via the hardware. If OldSettings != Settings, I change the settings on the hardware first.

How can I compare an old Settings class with the current Settings class if I implement it this way? Is this the best way for CommunicationMediator to check if the Settings class has changed?

Community
  • 1
  • 1
brazc0re
  • 253
  • 4
  • 13

1 Answers1

4

Your Settings class could just implement INotifyPropertyChanged. This is a standard interface in the framework which allows you to subscribe to notifications if properties within a class have changed, and is far simpler than deep copying and comparing each time.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thank you for the reply and link. This looks like a great solution; however, the only question I have is that not all of my properties are the type. For example, some properties are strings, some are custom enums, etc. In the example of the MSDN article, when the property is changed, it calls NotifyPropertyChanged(string x) , which then raises the event. Is there a way to change the string argument of NotifyPropertyChanged to something generic? Example of a property I have: public byte MessageLength { get; set; } public CommunicationType CommunicationType { get; set; } – brazc0re Jun 11 '12 at 16:42
  • 1
    @brazc0re This works for any type. The string you pass is the *name of the property*, and has nothing to do with the type. For example, you'd pass `new PropertyChangedEventArgs("CommunicationType")` for your last property. – Reed Copsey Jun 11 '12 at 16:46
  • Should have read the code more carefully. Makes sense now. Thanks again! – brazc0re Jun 11 '12 at 16:48