September 2007 - Posts

Short-circuit evaluation in C# and VB.NET

In my early years of college as a CS student I learned about short-circuit evaluation.  

Short-circuit evaluation denotes that the second argument is only evaluated if the first argument does not suffice the value of the expression. I found out that short-circuit evaluation in C# is different from VB.NET. Consider the following codes.

Here, if MyFunctionA() returns false and MyFunction() will not be executed.

if (MyFunctionA() && MyFunctionB())
{              

}

Same with this, if MyFunctionA() returns true, MyFunctionB() will not be evaluated.

if (MyFunctionA() || MyFunctionB())
{
 
//Some codes here         
}

In VB.NET, it’s different. Even if MyFunctionA() returns false, MyFunctionB() will also evaluated. There’s no short-circuit evaluation happened.

If (MyFunctionA() And MyFunctionB()) Then
 'Some codes here

End If

 
Same with this,

If (MyFunctionA() Or MyFunctionB()) Then
 'Some codes here

End If

 
Here’s the catch, to be able to achieve the short-circuit evalution in VB.NET use
AndAlso and OrElse keyword.

If (MyFunctionA() AndAlso MyFunctionB()) Then

If (MyFunctionA() OrElse MyFunctionB()) Then

 

I wonder why the keywords (instead of using And only and Or for VB.NET) are different from these 2 languages considering they were both from MS....Confused

Posted by n.ocampo with 7 comment(s)
Filed under: , ,

Debugging JavaScript code in ASP.NET

 

I am currently working with conversion of ASP Classic web application to ASP.Net 2.0 and I have to deal with a lot of client-side script. Before, I used to insert an alert(sampleValue) to be able to view the current value of a variable until I found out that about “debugger” keyword in JavaScript.

 
Here’s how to enable client debugging in ASP.NET.

 

  1. Uncheck the “Disable Script Debugging (Internet Explorer)“ options in Internet Explorer.

          IE Setting 

  1. Create a simple Web Form.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientScriptDebugging.aspx.cs" Inherits="ClientScriptDebugging" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
    <title>Client-Side Debugging</title>
<
script language="javascript" type="text/javascript">
<!--
function
btnOk_onclick() {
    var ColorCode;
    var ColorDescription;
    var Name;   
      
    debugger;   
    Name = document.getElementById("txtName").value;
    ColorCode = document.getElementById("drpColor").value;       
    for(i = 0; j < document.getElementById("drpColor").options.length; j++)
    {
        if(document.getElementById("drpColor").options[j].value == ColorCode)
        {
           ColorDescription = document.getElementById("drpColor").options[j].text;
            break;
        }
    }       
    alert("Hi, " + Name + ".\n You're favorite color is " + ColorDescription);   
}
 

// -->

</script>
</
head>
<
body>
    <form id="form1" runat="server">
    <div>
        <select id="Select1" name="drpColor">
            <option selected="selected" value="0">Red</option>
            <option value="1">Blue</option>
            <option value="2">Yellow</option>
            <option value="3">Green</option>
            <option value="4">Orange</option>
            <option></option>
        </select>
        <input id="btnOk" style="width: 66px" type="button" value="OK" language="javascript" onclick="return btnOk_onclick()" />
        <input id="txtName" type="text" /></div>
    </form>
</
body>
</
html


            Once you run the application and the compiler hits the debugger keyword, VS will automatically breaks on this line. You can now use the rich debugging tools of Visual Studio that we usually use when debugging server side codes.

 

       Watch Window

  

Posted by n.ocampo with 1 comment(s)
Filed under: , ,