Sharing a class between multiple forms

前端 未结 3 760
星月不相逢
星月不相逢 2021-01-23 05:39

I need help figuring this question out. I\'ve tried searching and I think I might have found a solution using a Singleton Design pattern, but want to make sure.

I have a

3条回答
  •  故里飘歌
    2021-01-23 06:23

    Actually, I've just ran into the same problem yesterday, and I think I might have found a much easier way of approaching this matter instead of using the singleton method. I made a class file and created a class called public class Serial. Inside I declared my serial variable as static SerialPort (i.e. static SerialPort serial); same with a timer if you need to use one. Create a function for set and get called serialStatus, where it sets a public static bool. I really would like to post my code here, but I really don't understand how to properly do it. In your form, you would have the first form start the serial port. For the other form, all you have to do is declare Serial serial = new Serial(); pull out the methods, and you're good to go. If you need to make changes to the serial settings, you can create a method with a bool input (i.e. if true, serial.Close(); then restart serial with new settings.

提交回复
热议问题