c# two nested if statements, one else -


please excuse question seems simple, reason cannot think of elegant solution @ moment.

i have following situation:

    if (request.querystring["name"] != null)     {         if (request.querystring["name"].tostring() != "")         {             namespan.innerhtml = request.querystring["name"].tostring();         }      } 

the problem is, if want hide namespan if querystring["name"] either null or emtpy. ugly solution be:

if (request.querystring["name"] != null) {     if (request.querystring["name"].tostring() != "")     {         namespan.innerhtml = request.querystring["name"].tostring();     }     else     {         namespan.visible = false;     } } else {     namespan.visible = false; } 

i have situation both namespan.visible = false; sections merged one area have write once.

as far aware, not possible following:

if (request.querystring["name"] != null && request.querystring["name"].tostring() != "") {     namespan.innerhtml = request.querystring["name"].tostring(); } else {     namespan.visible = false; } 

but please tell me if wrong! if have different solution changes logic more happy have different views! thank you!

your && solution should fine. if left side of && false, right side not evaluated there no exception.

if want, use string.isnullorempty static method:

if (!string.isnullorempty(request.querystring["name"])) {     namespan.innerhtml = request.querystring["name"].tostring(); } else {     namespan.visible = false; } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -