c#


After an hour or two of sleuthing I’ve discovered that jQuery 1.2.6 does not set the Content-Type header for HTTP GETs even if you explicitly use the contentType parameter. My understanding of the jQuery rationale is that GETs don’t contain data and therefore a Content-Type header is not required. Unfortunately, Microsoft reckons that GETs returning JSON provide a security risk and that a Content-Type header must be specified. Scott Guthrie explains it here http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx.  

After reading documentation from both camps I can fully understand their opposing views.  Ultimately it is yet another indicator that the responsibility of user protection lies in the browser is blatantly wrong!  If the browser raised a message asking the user if they are ok with a cross domain request and acted according to the response it would render both jQuery’s and Microsoft’s approaches obsolete.

So now I am caught between a rock and a hard place.  I love jQuery and I love Microsoft ASP.NET webservices and I am going to use both in my current projects.  But, if I can not explicitly set the Content-Type header for an .ajax() GET then I have just one choice and that is to use POSTs instead.  Unfortunately this contradicts the notion of using the correct HTTP verbs and removes the possibility of a RESTful API. 

It’s always about compromise.

I came across this handy shortcut earlier today.  It is the AND assignment operator and works just like +=.  I’ve used += in various C like languages for years but it never occurred to me that the same syntax could be used with other assignment types.

For example if we want to test a particular bit we might write

x = x & y;
when in actual fact we can save ourselves a keystroke and write
x &= y;

This makes the syntax for flipping a bit in an enum as simple as:

myEnum.bits &= ~someBit;

As a lazy developer I have come to rely heavily on ASP.NET’s Just-In-Time compilation model to catch my typos and mistakes. Sometimes it is easier to refresh the browser and let the ASP.NET engine report the location of a compile error than to eyeball each line of code and ensure a clean execution every time. This becomes especially apparent after the ninth or tenth hour of a long tiring day.

Today I’ve been battling a seemingly random error that would not react to source code changes properly. That is to say, I would make a source code change and not only would the expected behaviour not manifest, but the original behaviour would not disappear. I at least expected to see a different runtime error, not exactly the same one.

After scratching my head for twenty minutes, completely restarting my machine and scratching my head for a further twenty minutes I decided to take a step back and see what was actually going on.

It turns out it was a simple, obvious and down-right embarrassing mistake. I have a solution that usually contains a single project. However today I have added a second existing project to my solution. The second project implements an assembly that is not in the GAC and that the main project is dependent upon. I was making changes to the Assembly source code.

Hitting F5 in Firefox triggers a JIT compile on the server for the project hosting the website but does not chain to the Assembly source code. In hindsight this is totally obvious. After all I am hitting the web project not the solution.

The moral of my story? If you rely on a compiler to check your source code don’t get too complacent.

Worthy link also includes some sample real-world usage of a few of the tools.  The Snippet Compiler looks promising.  I’ve installed it.  We’ll have to see if I’m using it after a few weeks.

.NET Tools: Ten Must-Have Tools Every Developer Should Download Now — MSDN Magazine, July 2004

This is in response to the original “Escape Sequences in .NET Resource Files, by Boris Eligulashvili.

There is actually another way to insert escape sequences, such as newline characters—or actually any Unicode character, directly into the Resource string—without resorting to Replace or similar manipulations.

First, place the cursor at the insertion point. Next, hold down the Alt key and enter the decimal value of the Unicode character, starting with 0, using the numeric keypad.

For example, to insert a newline character (\u000a), hold down the Alt key while pressing 010 on the numeric keypad.

As another example, suppose you want to insert a registered (\u00ae) trademark symbol. You can either use the Character Map utility or hold down Alt while entering that value in decimal from the numeric keypad (0174).

How many of you remember using this technique in DOS to create inaccessible and obfuscated filenames?

Another Way to Escape Sequences in .NET Resource Files

After several very frustrating hours of work I discovered that C# constructors are not inherited.   To be more precise, constructors with parameters are not in inherited.  I had a situation where I had created a class called “tag” that provided 99% of the functionality required.  However, it was used by two other classes and both needed a different constructor.  I spent several hours going through the online help, websites and trying different approaches before I finally came across http://www.yoda.arachsys.com/csharp/constructors.html.  The last few paragraphs are quoted:

Constructors are not inherited

(ECMA) (MS), second paragraph from the bottom.
Constructors are not inherited. In other words, just because a base class has a constructor taking a certain list of parameters doesn’t mean that a derived class has a constructor taking that list of parameters. (It can, by providing one itself, but it doesn’t inherit it from the base class.) To demonstrate this, here’s an example which doesn’t compile:

public class MyBaseClass
{
    public MyBaseClass (int x)
    {
    }
}

public class MyDerivedClass : MyBaseClass
{
    // This constructor itself is okay - it invokes an
    // appropriate base class constructor
    public MyDerivedClass () : base (5)
    {
    }
              
    public static void Main()
    {
        new MyDerivedClass (10);
    }
}

Here, we try to invoke a constructor for MyDerivedClass which takes an int parameter. There isn’t one, however, as constructors aren’t inherited. The MyBaseClass constructor which takes an int parameter can be invoked by a constructor in MyDerivedClass (as is shown by the parameterless MyDerivedClass constructor) but isn’t actually inherited. Removing the “10” from the above code would make it compile and run with no problems – the parameterless MyDerivedClass constructor would be invoked, and that would in turn invoke the MyBaseClass constructor taking an int parameter, with 5 as that parameter value.

Some people have said that they would rather constructors were inherited, making the language act as if all derived classes had constructors with all the parameter lists from the constructors from the base class, and just invoking them with the parameters provided. I believe this would be a very bad idea. Take, for instance, the FileInfo class. You must logically provide a filename when constructing a FileInfo instance, as otherwise it won’t know what it’s meant to be providing information on. However, as object has a parameterless constructor, constructors being inherited would then mean that FileInfo had a parameterless constructor. Some have suggested that this could be fixed by allowing you to “override” the parameters you didn’t want invoked as private, but this goes against the idea that you should never be able to override anything to give it more restrictive access, and also means that class developers would have to change their code every time a new constructor was added to a base class.

Source: Constructors in C#

Funny how sometimes the simplest functions are the hardest to find. The other day I needed to test a value to see if it wa numeric. I stupidly assumed that I could write something like

string s = “123”;
if (isNumeric(s))
{
//do stuff
}

And quickly realized this wasn’t going to work when VisualStudio threw a compile time error. After (too) much digging I discovered that the function exists and it is actually a method of the char type. Therefore

string s = "123";
if(char.IsNumber(s))
{
//do stuff
}