Boolean operator structures in C #

my query is as simple as I am, if possible in C #.

<html>
<head>
<script src="../jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function(){
        $("#datos").append("<span style=\"font-weight:bold\">(false || null || \"1ΒΊ\" || \"2ΒΊ\")</span> : <span>"+(false || null || "1ΒΊ" || "2ΒΊ")+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(false && null || false || \"2ΒΊ\")</span> : <span>"+(false && null || false || "2ΒΊ")+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(false && true || \"1ΒΊ\" || \"2ΒΊ\")</span> : <span>"+(false && true || "1ΒΊ" || "2ΒΊ")+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(true && null && 6 && 5)</span> : <span>"+(true && null && 6 && 5)+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(null && true && false && false)</span> : <span>"+(null && true && false && false)+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(false && true && false && false)</span> : <span>"+(false && true && false && false)+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(true && 1 && true && 2)</span> : <span>"+(true && 1 && true && 2)+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(true && \"1ΒΊ\" && true && \"2ΒΊ\")</span> : <span>"+(true && "1ΒΊ" && true && "2ΒΊ")+"</span><br />");
        $("#datos").append("<span style=\"font-weight:bold\">(true && true && \"1ΒΊ\" && \"2ΒΊ\")</span> : <span>"+(true && true && "1ΒΊ" && "2ΒΊ")+"</span><br />");
    });
</script>
</head>
<body>
<div id="datos"></div>
</body>
</html>

      

Return to HTML:

(false || null || "1ΒΊ" || "2ΒΊ") : 1ΒΊ
(false && null || false || "2ΒΊ") : 2ΒΊ
(false && true || "1ΒΊ" || "2ΒΊ") : 1ΒΊ
(true && null && 6 && 5) : null
(null && true && false && false) : null
(false && true && false && false) : false
(true && 1 && true && 2) : 2
(true && "1ΒΊ" && true && "2ΒΊ") : 2ΒΊ
(true && true && "1ΒΊ" && "2ΒΊ") : 2ΒΊ

      

Thank.

- EDIT for comments

I need to do the following in C #:

string value1 = string.Empty;
string value2 = "default value";

// Request.Form["valueA"] and Request.Form["valueB"] may be null
value1 = (Request.Form["valueA"] || Request.Form["valueB"] || value2);

      

+2


source to share


3 answers


These expressions are not valid in C # because the tags are .NET ||

and &&

strongly typed.

However, there is an operator x ?? y

that returns y

if x

equal null

and x

otherwise:



string value1;
string value2 = "default value";

value1 = Request.Form["valueA"] ?? Request.Form["valueB"] ?? value2;
                                ^                         ^

      

+3


source


If I understand your "question" correctly, you need to understand that C # handles type in a much more restrictive way. Null is not considered to be false, in fact the only type you can use in a boolean expression is boolean, as opposed to javascript which will take any type into a boolean expression and make some reasonable guess as to what is meant.

The only operator that comes close to what you do in Javascript is ?: -

 var r1 = (null ?? "1ΒΊ" ?? "2ΒΊ") // Results in 1ΒΊ
 var r2 = (null ?? null ?? "2ΒΊ") // Results in 2ΒΊ

      

However, this doesn't work: -

 var r1 = (null ?? 1 ?? 2)  //Compile error

      



Integer values ​​are value types and cannot be null.

Editing solution

Whenever there is even the slightest hint of "processing" the querystring / form values ​​before using, I would remove it all inside the property. (Actually I try to place all such access in a property, even if it's simple {return Request.QueryString["stuff"]; }

).

 string _Value;
 public string Value
 {
    get
    {
       if (_Value == null)
       {
         _Value = CoalesceNullOrEmpty(Request.Form["valueA"],
           Request.Form["valueB"],
           "Default Value");
         }
       }
       return _Value;
    }
 }

//Can place this in the page but is more useful in a utility dll
public string CoalesceNullOrEmpty(params string[] values)
{
  foreach(string value in values)
    if (!String.IsNullOrEmpty(value))
      return value;

  return null;
}

      

Now use the Value property where you used the value1 property.

+1


source


If you want a series of bools, why not write a class that converts for you, applying the rules you want. If your rules are "if it's null it's false, if it's not null it's boolean otherwise it's true" then this method will work for you.

static List<bool> ToBool(FormCollection form) { // couldn't find the type on MSDN
    List<bool> result = new List<Bool>();
    foreach (object o in form) {
         if (o == null) {
             result.Add(false);
         }
         else {
             if (o is bool) {
                 result.Add((bool)o);
             }
             else {
                // do whatever other conversion
                result.Add(true); // probably the wrong thing - depends on what you're testing
             }
         }
     }
     return result;
}

      

0


source







All Articles