# Thursday, 04 September 2008

Last week I had to help someone out on the ASP.NET that was asking the question that he couldn't find the 2.0 bits anymore of the ASP.NET AJAX Control Toolkit. Indeed, when you navigate to the Releases tab of the project on Codeplex you'll only see this list:

Source Code AjaxControlToolkit-Framework3.5SP1.zip
source code, 3033K, uploaded Aug 21 - 24104 downloads
Application AjaxControlToolkit-Framework3.5SP1-NoSource.zip
application, 1716K, uploaded Aug 21 - 8256 downloads
Application AjaxControlToolkit-Framework3.5SP1-DllOnly.zip
application, 512K, uploaded Aug 22 - 5704 downloads
Application AjaxControlToolkit-ScriptFilesOnly.zip
application, 238K, uploaded Aug 22 - 2555 downloads
 
Phew, no more 2.0 bits. It seems that Microsoft decided to cut on keeping things rolling since SP1 of .NET 3.5 came out.
 
Luckily for the person I helped out you can still reach to the 2.0 bits by navigating to this url (the release of February 29th 2008).
 
Grz, Kris.
Thursday, 04 September 2008 13:51:08 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, 18 August 2006

I've written already some articles in the past about the null coalescing operator (1) but recently my colleague found some "strange" behavior when using this operator. First of all the guilty code:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Text;

    4 

    5 namespace ConsoleApplicationNullableTypes

    6 {

    7     public class NullCoalescingTestWithinObject

    8     {

    9         static void Main(string[] args)

   10         {

   11             int multiplicator = 5;

   12             int workRegime = 6;

   13 

   14             SomeObject so = new SomeObject();

   15             so.Test = 7;

   16 

   17             decimal result = (so.Test ?? 0 * multiplicator) / workRegime;

   18 

   19             Console.WriteLine(result.ToString("N"));

   20 

   21             Console.ReadLine();

   22         }

   23     }

   24 

   25     internal class SomeObject

   26     {

   27         private decimal? _test;

   28 

   29         public decimal? Test

   30         {

   31             get { return _test; }

   32             set { _test = value; }

   33         }

   34     }

   35 }

 The actual behavior for investigation is on line 17. The parenthesis are omitted around the statement so.Test ?? 0. This causes the following code to be produced (I used Lutz Roeder's .NET Reflector tool for this):

private static void Main(string[] args)
{
      int num2 = 6;
      SomeObject obj1 = new SomeObject();
      obj1.Test = new Nullable<decimal>(new decimal(7));
      Nullable<decimal> nullable1 = obj1.Test;
      Console.WriteLine(((nullable1.HasValue ? nullable1.GetValueOrDefault() : new decimal(0)) / ((decimal) num2)).ToString("N"));
      Console.ReadLine();
}

The output is 1.17 and not really what one would expect.

Now with the parenthesis placed around the statement so.Test ?? 0 so line 17 becomes:

   17 decimal result = ((so.Test ?? 0) * multiplicator) / workRegime;

The output becomes 5.83 and is what is expected. Now the following code is generated:

private static void Main(string[] args)
{
      int num1 = 5;
      int num2 = 6;
      SomeObject obj1 = new SomeObject();
      obj1.Test = new Nullable<decimal>(new decimal(7));
      Nullable<decimal> nullable1 = obj1.Test;
      Console.WriteLine((((nullable1.HasValue ? nullable1.GetValueOrDefault() : new decimal(0)) * ((decimal) num1)) / ((decimal) num2)).ToString("N"));
      Console.ReadLine();
}

For developers whom are still new to this new operator you must be aware of the operator precedence in C#(2). The * operator is normally executed before the ?? operator. But in the generated code we can clearly see that the num1 variable, in our code multiplicator, is omitted which causes some strange side effects to your code to kick in.

Grz, Kris.

(1): Testing upon Nullable types in C# 2.0 and Be sure to put in some default value when testing with Nullable types in .NET 2.0.
(2): operator precedence in C#

kick it on DotNetKicks.com

Friday, 18 August 2006 13:30:17 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, 21 July 2006

IIS7 hasn't shipped yet but will be available in Vista and Longhorn server. It provides better ways to extend it and configure it. There's also already some nice video content about the subject:

Microsoft also provides the possibility to test drive it on one of their Virtual Labs.

Grz, Kris.

.NET 2.0 | ASP.NET | Windows | IIS
Friday, 21 July 2006 09:13:00 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Monday, 10 July 2006

When ASP.NET 2.0 was still out of reach for most people I already found on the internet a great looking diagram I downloaded in the past. All credits go to the original designer of the diagram: Léon Andrianarivony. Because I couldn't find the original blogpost anymore I decided to share it with you, the readers of this blog. Enjoy!

Grz, Kris.

kick it on DotNetKicks.com

Monday, 10 July 2006 18:48:47 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [2]  | 
# Wednesday, 28 June 2006

Most people that use C# are quite familiar with the if() expression and the (?:) conditional/ternary operator. In C# 2.0, the Visual C# team introduced another operator that's less known: the ?? or null coalescing operator. A quick demo explains how to use it. The result can be seen in Figure 1:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Text;

    4 

    5 namespace ConsoleApplicationNullableTypes

    6 {

    7     class Program

    8     {

    9         static void Main(string[] args)

   10         {

   11             int? input = null;

   12 

   13             IfStatements(input);

   14 

   15             // Now fill up the nullable input with some value.

   16             input = 99999;

   17 

   18             IfStatements(input);

   19 

   20             Console.ReadLine();

   21         }

   22 

   23         /// <summary>

   24         /// Show the 3 different ways to write the same if functionality in C# 2.0.

   25         /// </summary>

   26         /// <param name="input"></param>

   27         private static void IfStatements(int? input)

   28         {

   29             int result;

   30             // Scenario 1, normal if statement

   31             if (input.HasValue)

   32                 result = input.Value;

   33             else

   34                 result = 200;

   35 

   36             Console.WriteLine("Scenario 1, result: " + result.ToString());

   37 

   38             // Scenario 2, using the conditional/ternary operator

   39             result = input.HasValue ? input.Value : 200;

   40 

   41             Console.WriteLine("Scenario 2, result: " + result.ToString());

   42 

   43             // Scenario 3, using the null coalescing operator new to C# 2.0.

   44             result = input ?? 200;

   45 

   46             Console.WriteLine("Scenario 3, result: " + result.ToString());

   47             Console.WriteLine();

   48         }

   49     }

   50 }

The 3 different manners of writing the same test. Scenario 1, line number 31, is the normal if statement. Scenario 2 uses the conditional/ternary operator and just like in scenario 1 the new method HasValue is used to check if the nullable type has a value. If so then one could fetch the value by using the Value property on the nullable type.
Scenario 3, line number 44, uses the new null coalescing operator which is only available in C# 2.0.

Since I started developing with C# 2.0 in May 2005 I used to use the code like described in scenario 2 until I found out about the null coalescing operator. I find it easier to read and to use in my code. But because I found out that not too many people seem to be aware of this new gem in the C# 2.0 language I wanted to show it off in an article.


Figure 1: The result of the test.

I also blogged about using testing serious with nullable types in the past: Be sure to put in some default value when testing with Nullable types in .NET 2.0.

Grz, Kris.  

kick it on DotNetKicks.com

Wednesday, 28 June 2006 20:59:22 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Sunday, 18 June 2006

Nullable types in .NET 2.0 are a great asset for many developers. I use them all the time in my current project. However there are some things that you should be aware of. Because they can be filled in or can be null you should perform some extra checks in order to get your code to behave like you would expect it.

I've written some sample code that uses a nullable DateTime endDate. In applications this can be used to denote a period, startDate - endDate with the endDate being as such that it will never expire. In .NET 1.x we would've just filled it up with new DateTime(9999, 12, 31);. With nullable types you can just let it be null and check appropriately.

    1 <%@ Page Language="C#"%>

    2 

    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    4 

    5 <script runat="server">

    6 

    7     protected void Page_Load(object sender, EventArgs e)

    8     {

    9         DateTime startDate = newDateTime(2004, 5, 23);

   10 

   11         // Using a nullable DateTime can represent an occurance that doesn't expire.

   12         // In a database, like SQL Server, you can have this field set to NULL.

   13         DateTime? endDate = null;

   14         DateTime toCheck = newDateTime(2006, 6, 13);

   15 

   16         // First example: this test will fail because we check

   17         // a normal DateTime against a nullable DateTime without

   18         // replacing it with something to check against when it's null.

   19         // Note however that this code compiles! It's only not working out

   20         // in a functional logic way because it doesn't do what we would expect.

   21         if (startDate <= toCheck && toCheck < endDate)

   22             LabelEndDateNull.Text = true.ToString();

   23         else

   24             LabelEndDateNull.Text = false.ToString();

   25 

   26 

   27         // Second example: now we're checking, and replacing it with an appropriate

   28         // value in case it's null, so the test will pass as expected.

   29         // The trick here is to use the GetValueOrDefault() method on a Nullable type

   30         // to replace it with a default value in case it's null.

   31         if (startDate <= toCheck && toCheck < endDate.GetValueOrDefault(DateTime.MaxValue))

   32             LabelEndDateNullButWithGetValueOrDefaultUsage.Text = true.ToString();

   33         else

   34             LabelEndDateNullButWithGetValueOrDefaultUsage.Text = false.ToString();

   35 

   36         // Third example: this one's exactly the same as the previous, second, example.

   37         // Only here I'm applying the great ?? operator available in C# 2.0.

   38         if (startDate <= toCheck && toCheck < (endDate ?? DateTime.MaxValue))

   39             LabelEndDateNullButWithCheck.Text = true.ToString();

   40         else

   41             LabelEndDateNullButWithCheck.Text = false.ToString();

   42 

   43 

   44         // Fourth example: fill up the nullable DateTime with some actual value:

   45         endDate = newDateTime(2007, 1, 1);

   46 

   47         // We can see, this is easier when done debugging the code, that the enddate

   48         // now has some actual value and the test will pass.

   49         if (startDate <= toCheck && toCheck < (endDate ?? DateTime.MaxValue))

   50             LabelEndDateNotNull.Text = true.ToString();

   51         else

   52             LabelEndDateNotNull.Text = false.ToString();

   53     }

   54 

   55 </script>

   56 

   57 <html xmlns="http://www.w3.org/1999/xhtml">

   58 <head runat="server">

   59     <title>Untitled Page</title>

   60 </head>

   61 <body>

   62     <form id="form1" runat="server">

   63     <div>

   64         Enddate = null:

   65         <asp:Label ID="LabelEndDateNull" runat="server"></asp:Label>

   66         <br/>

   67         <br/>

   68         Enddate = null but with GetValueOrDefault method:

   69         <asp:Label ID="LabelEndDateNullButWithGetValueOrDefaultUsage" runat="server"></asp:Label>

   70         <br/>

   71         <br/>

   72         Enddate = null but with ?? check:

   73         <asp:Label ID="LabelEndDateNullButWithCheck" runat="server"></asp:Label><br/>

   74         <br/>

   75         EndDate != null:

   76         <asp:Label ID="LabelEndDateNotNull" runat="server"></asp:Label></div>

   77     </form>

   78 </body>

   79 </html>

The output of this little code sample is:

Enddate = null: False

Enddate = null but with GetValueOrDefault method: True

Enddate = null but with ?? check: True

EndDate != null: True

 

Take a look at the comment in the code, or better yet: debug it on your own dev machine, to see the subtle difference between the first and second example. The third example's just the same as the second but uses the ?? operator, the one that I prefer in my code because its shorter. The ?? operator, or null coalescing operator, is only available in C# 2.0.

Grz, Kris.

kick it on DotNetKicks.com

Sunday, 18 June 2006 13:01:24 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |