c# - Why is a bool's default val (false) not recognized? -
this question has answer here:
with code:
bool successfulsend; const string quote = "\""; string keepprinteron = string.format("! u1 setvar {0}power.dtr_power_off{0} {0}off{0}", quote); string shutprinteroff = string.format("! u1 setvar {0}power.dtr_power_off{0} {0}on{0}", quote); string advancetoblackbar = string.format("! u1 setvar {0}media.sense_mode{0} {0}bar{0}", quote); string advancetogap = string.format("! u1 setvar {0}media.sense_mode{0} {0}gap{0}", quote); if (radbtnbar.checked) { successfulsend = sendcommandtoprinter(advancetoblackbar); } else if (radbtngap.checked) { successfulsend = sendcommandtoprinter(advancetogap); } if (successfulsend) { messagebox.show("label type command sent"); }
i get, "use of unassigned local variable 'successfulsend'"
so have change bool declaration to:
bool successfulsend = false;
...to compile/run. isn't false default value of bool[ean]s? why default value have explicitly specified?
there no complaint code:
public bool sendcommandtoprinter(string cmd) { bool success; // init'd false default try { serialport serialport = new serialport(); serialport.baudrate = 19200; serialport.handshake = handshake.xonxoff; serialport.open(); serialport.write(cmd); serialport.close(); success = true; } catch // may not need try/catch block, success defaults false { success = false; } return success; }
update
this compiles:
bool success; serialport serialport = new serialport(); serialport.baudrate = 19200; serialport.handshake = handshake.xonxoff; serialport.open(); serialport.write(cmd); serialport.close(); success = true; return success;
but isn't false default value of bool[ean]s?
for fields (instance variables , static variables), yes.
but local variables don't have default values, regardless of type. have assigned before they're read.
Comments
Post a Comment