Comparing strings in C# with OR in an if statement -
i'm newbie c#, can't seem find problem. here's i'm trying do:
string teststring = txtbox1.text; string teststring2 = txtbox2.text; if ((teststring == "") || (teststring2 == "")) { messagebox.show("you must enter value both boxes"); return; }
basically need check see if either txtbox1 or txtbox2 blank. i'm getting error when running this. what's proper way (or approach wrong)?
since want check whether textboxes contains value or not code should job. should more specific error having. can do:
if(textbox1.text == string.empty || textbox2.text == string.empty) { messagebox.show("you must enter value both boxes"); }
edit 2: based on @jonskeet comments:
usage of string.compare not required per op's original unedited post. string.equals should job if 1 wants compare strings, , stringcomparison
may used ignore case comparison. string.compare should used order comparison. question contain comparison,
string teststring = "this test"; string teststring2 = "this not test"; if (teststring == teststring2) { //do stuff; }
the if statement can replaced
if(teststring.equals(teststring2))
or following ignore case.
if(teststring.equals(teststring2,stringcomparison.invariantcultureignorecase))
Comments
Post a Comment