c# - Pass variable from event handler to another method -
i working on .net application gets message serial port (arduino) received event handler. unable pass message stored event handler method. event handler receives data looking this:
private static void messagereceivedhandler(object sender, serialdatareceivedeventargs e) { serialport serialport = (serialport)sender; string received_data = serialport.readexisting(); // pass received_data method }
i want received_data
variable passed method named getmessage()
. method execute operations using received data returned. getmessage()
called class, these operations can not implemented in event handler.
edit : sorry, missed important point here. want received_data available in getmessage without getting parameter. because class needs access getmessage without (out output_data)
parameter.
public bool getmessage(out output_data) { bool success = true; // part not understand how implement string input_data = received_data; try{ // operations input_data (which data event handler). } catch (exception e) { console.writeline(e.tostring()); succcess = false; } output_data = input_data; return success; }
i assume possible have received_data
global variable , read/write accordingly. however, not approach, advice in order find solution.
since don't want use received_data parameter believe best choice global variable. can still use parameters if problem need method being called somewhere else. more complicated way method used parameters:
public bool getmessage(out output_data, string received_data, bool receiveddataneeded) { bool success = true; if(receiveddataneed){ // part not understand how implement string input_data = received_data; try{ // operations input_data (which data event handler). }catch (exception e){ console.writeline(e.tostring()); succcess = false; } }else{ string input_data = "whatever need initialize to"; try{ // operations input_data (which data event handler). } catch (exception e){ console.writeline(e.tostring()); succcess = false; } } output_data = input_data; return success; }
when call getmessage handler can call this:
getmessage(output_data, received_data, true);
when want call somewhere else don't need received_data parameter can call this:
getmessage(output_date, "", false);
Comments
Post a Comment