DCSIMG
May 2009 - Posts - Pini Dayan

Pini Dayan

The best thing about a boolean is even if you are wrong, you are only off by a bit.

May 2009 - Posts

VS.NET 2010 cool new features

I installed today the VS.NET 2010 beta 1 edition on my laptop. Here are some new feature I have discovered by playing a bit with the IDE and reading about it here.

 

1. When editing you can zoom in into your code (Good for lecturing purposes or if you need a better glasses).
Zoom out image   or zoom in     image

2. Call Hierarchy: unlike the call stack that is available during runtime. The Call Hierarchy is available  during design time.Using this nice feature you can see:

  • Where are the calls to and from a method, property, or constructor.

  • What interfaces are being implemented

  • What virtual or abstract member are being override.

    (You can use the Call Hierarchy by selecting a method, right clicking it and choosing View Call Hierarchy or you can use the short cut CTRL+ALT+K)

  • image

    You can also now open any node for any function and see who is calling and who this function calls

     

    To learn more new features ,

    What's New in the Visual Studio 2010 Editor

     Call Hierarchy

    Posted: May 21 2009, 02:10 PM by Pini Dayan | with no comments |
    תגים:

    C#, Converting string into "Title Case"

    Here is a nice a simple way I found to make a string "Title Case". What this means is that every word in a string will have it's first letter upper cased.

    So, Here are the steps to accomplish this simple task:

    1. Add using System.Globalization; to your code.

    2. Create a TextInfo object. It has a ToTitleCase function we shall soon use.

    TextInfo info = CultureInfo.CurrentCulture.TextInfo;

    3. Convert the string you need to "Title Case".

    Here is a simple sample:

    static void Main(string[] args)
    {
        TextInfo oInfo = CultureInfo.CurrentCulture.TextInfo;
        string sample = "the Is a nice way Of converting a STRing intO a title Case";
        Console.WriteLine(oInfo.ToTitleCase(sample));
    }

    And the result will be: The Is A Nice Way Of Converting A String Into A Title Case.

    Enjoy.

    Posted: May 13 2009, 08:44 AM by Pini Dayan | with 1 comment(s) |
    תגים:,

    Complete solution for minimizing and choosing the debug/release versions for js files

    Hi all, As I mentioned in earlier posts I Wrote,One of the greatest thing in Microsoft Ajax is it's ability to download a very small js files it needs. The files are very small since Microsoft used a nice way to minimized the files for the release versions. What they did is very simple, In the System.Web.Extentions DLL you can see in the resources (Using .NET reflector) that there are 2 js versions for each file : The debug version and the release version. During runtime they will download the file you need according to your compilation settings : If it's debug , then the full documented and well formed version will be used , otherwise the shrinked version.

    So I have decided to do it in my web projects as well. So how do you do it:

    Step 1:

    Write your original js files , for simplicity , let's say we have one file called: JScriptdebug.js which contains a very small function:

    ///<summary>
    ///This function will show a message box with some text
    ///</summary> 
     function ShowMessage() {
        alert("Some message from the debug version");
    }

    Step 2:

    We now wish to shrink this file by removing it's comments and by deleted line breaks etc. We can do it by using the JSMIN utility available here. Once we downloaded the file we can run it in the cmd window: "jsmin <JScriptdebug.js> JScript.js " and we see that a new file by the name JScript.js is created and it's source is :

    function ShowMessage(){alert("Some message from the debug version");}

    Nice right? Now imaging this file had many lined of code....

    Step 3:

    Now we need a way to make sure that once we are in debug mode the debug js version files will be used so we can debug easily and read the file easily. On the other hand , once we are deploying our web application/web site we use the smaller version. So in order to do so, here is a nice trick to archive this.

    Whenever I want to register these file and include them in some aspx header I will use ClientScript in the code behind to register this include. (Of chores a more practical way is to do it in the master page or some base page):

    Page.ClientScript.RegisterClientScriptResource(typeof(_Default), "DebugRelease.JavaScript.JScript.js");

    But we need something elsem since we want it to be dependent in the compilation setting, so here is the solution (Thank you Shlomo for the help there):

    #if DEBUG
    Page.ClientScript.RegisterClientScriptResource(typeof(_Default), "DebugRelease.JavaScript.JScriptdebug.js");
    #else
    Page.ClientScript.RegisterClientScriptResource(typeof(_Default), "DebugRelease.JavaScript.JScript.js");
    #endif                       

    When the #if and the #else are used it will compile the code between the directives only if the specified symbol is defined.(These are preprocessor directives).

    Now all you have to do if you want a complete solution is to create a post build event (in your web application) and make sure the jsmin runs every time you complie.

    Enjoy.

     

    Windows 7 - What is new?

    Here are 2 part video from the msdev.com site explaning what Windows 7 has to offer both for end users and for developers.

    Part 1:


    Part 2:

    JavaScript Date localization problems

    Sometimes we have to compare dates, sort some table according to some column containing grid in the client side. This brings us to try and compare dates. The problem arises when we get a solution but then fining out that if the regional settings in our machine will change the problem arises again. There are many solutions to solve these problems on the net and you can find them out when looking for "JavaScript datediff" in Google. Today I decided to find a nicer and more elegant solution , so here it is:

    Let's say you have a string contains a date such as "05/04/2008". If we try to use the "constructor function" of the Date object in JavaScript like:

    var oDate = new Date(sDate);

    We will notice that whenever we change the regional setting to some other culture, the date object will be different. This is because this string can be read as "dd/MM/YYYY" or as "MM/dd/YYYY". (Depending on the culture)

    So , here is a nice and simple way to initialize a date object correctly:

    function GetDateByStringUnLocalized(sDateStr) {
           var oDate = new Date();
           
           //Parse the date according to your string
           var oDateARR = sDateStr.split("/");
           oDate.setDate(oDateARR[0]);
           oDate.setMonth(oDateARR[1]);
           oDate.setFullYear(oDateARR[2]);
           return oDate;
       }
    This assumes you as a programmer knows how you constructed the date string.

    JavaScript parseInt method - be careful

    Today I solved  a bug in JavaScript for a company I am advising to. The bug had to do to the simple fact that they did not  really understood the ParseInt method. The special thing to notice about this method is the radix (The base ) in which the method works with.

    As it turns out, the parseInt method not only gets a string as a parameter and tries to convert it to integer, it can also get a second optional parameter which is the radix. (from 2 to 36). Why is this radix so important? Simple. Because if the string begins with "0" then the function will return "0" no matter what number follows it, this is because the default radix in this case is 8(octal).

    So for example:

    var sNumber = "8";
    alert(parseInt(sNumber));
    will alert "8"     
    var sNumber2 = "08";
    alert(parseInt(sNumber2));will alert "0".

    The last line should have been written like this:  alert(parseInt(sNumber2,10));

    So next time you are trying to convert a string into int - pay attention.

    JavaScript method:splice. The most unused function ever

    As it turns out , JavaScript Array type (Or actually prototype) has a method called "splice".
    I don't think many programmers out there  ever heard of this function.

    unused-gadgets

    The signature for this function is: splice(start, delCount, [arg1[,arg2[,…]]]) and what it does it:

    Deletes from array delCount elements starting at position start, Replaces the deleted elements with optional arguments provided after delCount. Lets see some sample:

    var oScriptionARR = new Array("Lets","Do","Some","Simple","Scripting");
    oScriptionARR.splice(2, 2,"good","javascript");
    alert(oScriptionARR);

    In this sample the result will be "Lets Do good javascript Scripting". What happened here is that 2 elements from the position 2 in the array were deleted and replaced with the 2 given arguments.

    I will be very happy to hear if anyone ever used this method?