c# - Assign event handling delegate in static factory method -
i suspect shouldn't using static factory method here, mean time @ loss how implement this. following code gives compile error on line in createopenport
assigns _currentport_datareceived
because delegate method isn't static. how can resolve this, preferably using non-static factory method?
public static serialportservice createopenport(string portname, int baudrate, parity parity, int databits, stopbits stopbits) { var service = new serialportservice { _currentport = new serialport(portname, baudrate, parity, databits, stopbits) }; service._currentport.datareceived += currentportcharsreceived; service._currentport.open(); return service; } void currentportcharsreceived(object sender, serialdatareceivedeventargs e) { var port = (serialport) sender; var chars = new char[port.bytestoread]; var x = port.read(chars, 0, chars.length); ondatareceived(chars.tostring()); }
hmm, static factory static handler not bad idea although try avoiding them sake of unit tests.
implement singleton pattern if there going 1 instance in appdomain. clean , working.
Comments
Post a Comment