tags:

views:

126

answers:

3

Hello, my query is simple, as I do, if possible, the following 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 you.

-- EDIT for Comments

I need to accomplish 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);
+3  A: 

Those expressions are invalid in C# because the .NET || and && operators are strongly typed.

There is, however, the x ?? y operator which returns y if x is null and x otherwise:

string value1;
string value2 = "default value";

value1 = Request.Form["valueA"] ?? Request.Form["valueB"] ?? value2;
                                ^                         ^
dtb
+1. But what happens if the url contains "?valueA=)
AnthonyWJones
@Anthony Nothing will happen a string "" does not affect the ?? nor and variables
Jonathan Shepherd
EDIT: and = any
Jonathan Shepherd
stevemegson
@Jonathan: Just to clarify: valueA ?? valueB where valueA = "" results in "" regardless of the value of valueB. This may or may not be desirable.
AnthonyWJones
@Jonathan: BTW, if you want to make a correction to a comment rather than adding another comment containing errata, copy'n'paste the original comment text into a new comment, correct it and add it. Then hover over your original comment and click the little delete icon.
AnthonyWJones
+1  A: 

If I understand your "question" correctly you need to understand that C# handles type in much stricter manner. Null is not treated as false, in fact the only type you can use in a boolean expression is a boolean, unlike javascript which will accept any type into a boolean expression and make some reasonable guess about what is meant.

The only operator that gets close to what you are doing 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

Integers are value types and cannot be null.

Solution to Edit

Whenever there is the slightest hint of "processing" of querystring/form values before use I would tuck it all away inside a property. (In fact I tend to place all such access in a property even if its just {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 would have used the the value1 property.

AnthonyWJones
A: 

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 non-null it's the boolean value, 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;
}
plinth