Month: October 2010

VB.NET: Return vs Exit Sub

The other day I was suddenly wondering which one is better for exiting a Sub, return or exit?

If you inspect the IL output of the 2 statements, they are the same. However, since  ‘return’ is meant for pushing something back to the caller, so strictly speaking, ‘Exit Sub’ is more suitable for using in a Sub. See the discussion here.

PS. IL is an assembly language usually used to debug .NET code at low level. See here for more.

T-SQL: CASE doesn’t go well with NULL

I recently came across a situation that requires the following simple case:

SELECT CASE Len(Article.Title)
WHEN 0 THEN 'No Title'
ELSE Article.Title END
AS Title

This will not work if the article title is a NULL value, an even simpler test can easily illustrate the problem:

SELECT CASE NULL
WHEN NULL THEN 'Null'
ELSE 'Not null' END

This will always return ‘Not null’. The CASE statement simply doesn’t work with NULL values. What I did to get around this problem is by using the Isnull function:

SELECT CASE Isnull(NULL, 0)
WHEN 0 THEN 'Null'
ELSE 'Not null' END

Update: jba has pointed out a critical performance issue with this method. The ISNULL function will be evaluated for each row of data that the query is processing. This can effectively turn any index-able query to use a table scan. Thanks for the tip John 🙂

JavaScript: Creating inline DOM objects

Honestly, it’s been a long time since I touch any javascripts but they still remain fun and powerful for me. Recently I came across a situation that I need to create some DOM objects using javascript. After a little search, I found out that it’s exactly how JSON objects are created:

var myObj =
[{
  title: 'My Object',
  dataList:
  [
    {name: 'data 1', value: 'value 1'},
    {name: 'data 2', value: 'value  2'}
  ]
}]

VBScript: “functionName = value” vs. “return value”

In VB.NET, it’s quite common to see people returning value for a function by assigning value to the function name:

Function GetNumber as Integer
  GetNumber = 15
End Function

This is equivalent to:

Function GetNumber as Integer
  return 15
End Function

The difference between this 2 methods is that if you assign a value to the function name, the function does not exit until it hits the end of the function or hits the Exit Function statement, where if you return the value, the function exits at that point.

Assigning value to function name is a legacy of the VB world and using the return statement is more conventional.

Check out more details here:  http://msdn.microsoft.com/en-us/library/x7hbf8fa(VS.85).aspx

ASP.NET: Returning CSS to client

I wasn’t a heavy back end guy but I’ve got my hands on some server control now, which returns CSS to another aspx page that expects the CSS in a <link/> tag in the page head. Something like this:

<link rel="stylesheet" type="text/css" href="Style.aspx?id=15" />

And code file of the Style.aspx page is as simple as:

string style = string.Empty;
// ...
// Grab style from DB.
// ...
Response.Clear();
Response.Write(style);
Response.End();

I realised that none of the browsers but IE recognises the styles correctly.

After a bit of trial and error + research, I found out that it was because I didn’t set the web response header correctly before return it.

What I was missing is just one line of code before calling the end for the response:

Response.AddHeader("Content-Type", "text/css");

I also found that you can’t use Response.Headers.Add("", "") to add the header entry, but didn’t go deeper looking into the reason though 🙂