if statement - How to break if-else-if using C# -
how break if-else-if.....why not working? checking conditions instead of performing tasks. following code. have checked through breakpoints moving conditions why doesn't stop after meeting correct condition. not going if activity read conditions , nothing @ end.
private void showhash() { inpic = pb_selected.image; bitmap image = new bitmap(inpic); byte[] imgbytes = new byte[0]; imgbytes = (byte[])converter.convertto(image, imgbytes.gettype()); string hash = computehashcode(imgbytes); txt_selectedtext.text = hash; gethash(); } private void gethash() { if (txt_sel1.text == null && (txt_sel2.text == null || txt_sel3.text == null || txt_sel4.text == null || txt_sel5.text == null )) { txt_sel1.text = txt_selectedtext.text; return; } else if (txt_sel1.text != null && (txt_sel2.text == null || txt_sel3.text == null || txt_sel4.text == null || txt_sel5.text == null)) { txt_sel2.text = txt_selectedtext.text; return; } else if (txt_sel2.text != null && (txt_sel3.text == null || txt_sel4.text == null || txt_sel5.text == null)) { txt_sel3.text = txt_selectedtext.text; return; } else if (txt_sel3.text != null && (txt_sel4.text == null || txt_sel5.text == null)) { txt_sel4.text = txt_selectedtext.text; return; } else if (txt_sel4.text != null && (txt_sel5.text == null)) { txt_sel5.text = txt_selectedtext.text; return; } }
reason:
if there nothing in textboxes, textbox.text
return empty string (""
) not null
.
solution:
check against ""
not null
:
private void gethash() { if (txt_sel1.text == "" && (txt_sel2.text == "" || txt_sel3.text == "" || txt_sel4.text == "" || txt_sel5.text == "")) { txt_sel1.text = txt_selectedtext.text; return; } else if (txt_sel1.text != "" && (txt_sel2.text == "" || txt_sel3.text == "" || txt_sel4.text == "" || txt_sel5.text == "")) { txt_sel2.text = txt_selectedtext.text; return; } .... ....
edit: don't have ==true
boolean variables. if statement checks against true default. use !
check against false
:
if (hasvalue1 && (hasvalue2 || hasvalue3 || hasvalue4 || hasvalue5)) { txt_sel1.text = txt_selectedtext.text; return; } else if (hasvalue2 && (!hasvalue1 ||hasvalue3 || hasvalue4 || hasvalue5)) { txt_sel2.text = txt_selectedtext.text; return; } else if (hasvalue3 && (!hasvalue1 || hasvalue2 || hasvalue4 || hasvalue5)) { txt_sel3.text = txt_selectedtext.text; return; } .... ....
Comments
Post a Comment