Wednesday, June 24, 2009

C# coalesce, double question-mark (??) operator

I have been coding in C# for years now but never knew this - the coalesce like operator in C#.


If I had to check a value with null before using it, I use the well known ternary operator (? :). But C# made our lives easier by introducing ?? operator for this task. For example, if you have a variable isParent and need to check for null, you can do:
isParent != null ? isParent : default_value;

Now, with the new ?? operator, we can write the same as:
isParent ?? default_value;

With the above expression, isParent will be used only if it is not null otherwise, default_value will be used. Its clean, isn't it? This might be a simple operator but it sure saves us from introducing few errors specially when we have some checks with nested ternary operators.

PS: This was introduced with nullable types in C#, and is available from .Net 2.0.

Wednesday, June 17, 2009

Java Script and Array.Sort()


I was working on porting some enhancement to new version and found some interesting javascript code similar to this:

array.sort(sortorder);

First I didn't get what that parameter ( sortorder) is, then a careful look revealed that it is a function that is written to change the default sort behavior and add some custom sort logic.

function sortorder(a, b){
//Compare a, b with some logic and return a value (-1, 0, 1)
}

In order to understand this correctly, I researched a bit and found some interesting points with the sort() function of javascript. By default this function sorts the array alphabetically (if we don't provide any parameter). It would work for you if you want to sort a string/character array in ascending order. If you want to change the order of sorting, you might need to use reverse function like:

array.reverse();

In case of sorting array with numbers, the default sort would give some unexpected surprises. It would again sort alphabetically so number like 5 would come after a number like 23. To get away with this and do the correct sort, you can define your own sort function and pass the function name as parameter to array.sort(). For example, following function would sort the numbers in ascending order:

function sortorder(a, b){
return a-b;
}

If you want to change the order to descending:

function sortorder(a, b){
return b-a;
}

Its as simple as that. You can even return -ve or 0 or +ve value randomly to randomize (or shuffle) the array. But beware of the performance impact of this.

Tuesday, June 16, 2009

Chat with your yahoo buddy from MSN messenger!

I use MSN IM at work. The other day a colleague approached me and asked me to add him in my buddy list. And he gave me his Yahoo mail id. I told him I use only MSN. But he gently smiled and educated me that MSN/Live messenger can add Yahoo contacts and vice versa. I couldn't believe that but when tried, it worked magically!




Then I visited yahoo messenger page and confirmed that it officially works:



Then I was wondering will someday google join this and extend its gtalk to work with MSN and Yahoo? I am not sure about this but heard that it has plans to support integration with AIM in near future. A little bit of googling tells me that we can tweak gtalk to work with MSN and Yahoo IMs easily. If you are interested in this, check out this article!

Monday, June 15, 2009

How to delete a windows service?


I had to work with a windows service few days ago. An application creates a windows service which does some work in the background. During the development, we ended up creating multiple services with different names. For those of you who don't know where to look for these, just open up the command prompt from your windows box and type services.msc




In my case, I was managing my task by stopping/disabling the old services and starting/enabling the one that is in use. But the list grew and the management task is getting ugly. Then I came to know about a simple command to get rid of these useless entries/services.


From command prompt, invoke the service control manager with SC command and with corresponding parameters. In my case, I had to give 'SC delete [service name]'


To see various parameters available hit SC without any parameter. Hope this helps you when dealing with services.


Friday, June 12, 2009

Regular Expression and URL Manipulation Attacks!

The other day I was involved in fixing a bug on a page. Actually, that bug was a regression after fixing a url manipulation attack. The jscript.net line of code that caused the issue is:

str = str1.replace(/[^\w]/ig, "") ;

All this does is to check the string (part of the url) for any non-alphabetical characters and replace any such characters with empty string. To explain the regular expression:

^ denotes the negative of what follows it is equal to saying 'Not ...'
\w denotes any of the alphabets and digits
i this switch denotes the ignore case option
g this switch denotes the global replace option

Now, the expression would be self explanatory. It is searching for all characters except alphanumeric ones and replacing any such with "" irrespective of case and in the whole string.

The issue we faced is, we had certain field in query string which takes a negative value. Even the minus (-) sign infront of the value is getting replaced with empty string and the page is going for a toss. I have fixed the issue by correcting the regular expression as:

str = str1.replace(/[^\w\-]/ig, "") ;

As you can see, I have included the minus sign also in the 'Not ..' list. Not sure, if there are any other hidden issues with this.. Do you have abetter solution to tackle this issue? Do share your comments..

Thursday, June 11, 2009

Oracle - Enabling Debugging for Store Procedures

The other day I was trying to debug a stored procedure in Oracle (using PL SQL Developer tool) and was greeted with the following error when I tried debugging:




[Error: ORA-0131: Insufficient privileges]


As the error says, the user id that I was using doesn't have debug permissions. To resolve this:
  • Goto command prompt (Start -> run -> cmd)
  • Hit command 'sqlplus'
  • Then connect to db as system admin using conn system/@;
  • You can then use grant debug connect session to ; to grant user the debug permission.
  • From PL SQL Developer tool, right click on the package and select 'Add debug information'.
  • Start debugging (hit F9).