Category: Programming

  • Math.Round

    Math.Round has been improved in C#.
    Consider the below piece of code:

    Console.WriteLine(Math.Round(10.4)); // Rounds to 10.0
    Console.WriteLine(Math.Round(10.7)); // Rounds to 11.0
    Console.WriteLine(Math.Round(10.5)); // Rounds to 10.0
    

    There is nothing surprising about the first two statements.
    In the third statement however, 10.5 is rounded to 10 not 11. C# provides for a way to specify how the middle point has to be treated.
    A enumeration ‘MidpointRounding’ that defines how mid points are treated.

    Console.WriteLine(Math.Round(10.5, MidpointRounding.AwayFromZero)); // Rounds to 11.
    Console.WriteLine(Math.Round(10.5, MidpointRounding.ToEven)); // Rounds to 10.
    Console.WriteLine(Math.Round(11.5)); //Rounds to 11.
    Console.WriteLine(Math.Round(11.5, MidpointRounding.AwayFromZero)); // Rounds to 12.
    Console.WriteLine(Math.Round(11.5, MidpointRounding.ToEven)); // Rounds to 12.
    

    ‘AwayFromZero’ – Rounds the number to the next highest value.
    ‘ToEven’ – Rounds the number to Even number.

    So for a odd fraction, the default round will round it to the lesser number and any of the above overloads will take it to the Next number.

  • Implicit Variable in C# 2008

    C# 2008 allows for creating implicit variables using the ‘var’ keyword. But the usage of ‘var’ cannot be truly justified for just declaring a ‘int’ or ‘string’ in code as shown in example below.

    var intNum = 5;
    
    Console.WriteLine("intNum is a: {0}", intNum.GetType().Name);
    Console.WriteLine("intNum is defined in: {0}", intNum.GetType().Namespace);
    

    The output of the above program is

    intNum is a: Int32
    intNum is defined in: System
    

    Here instead of using int in the declaration, we have used ‘var’ keyword. There is no difference between using ‘int’ or ‘var’ in the above example.
    The real usage of ‘var’ comes in LINQ. Consider the following code.

    int[] numbers = { 10, 20, 30, 40, 8, 7, 6, 2 };
    var resultSet = from i in numbers where i < 10 select i;
    
    foreach (var i in resultSet)
    {
    Console.Write("{0}", i);
    }
    
    Console.WriteLine("resultSet is a: {0}", resultSet.GetType().Name);
    Console.WriteLine("resultSet is defined in: {0}", resultSet.GetType().Namespace);
    

    Here resultSet is declared as a ‘var’. From the code, we understand that anytime, the resultSet will be an array of integers. But ‘resultSet.GetType().Name’ gives a surprising result.

    resultSet is a: d__0`1
    resultSet is defined in: System.Linq
    

    So ‘var’ has its best usage in LINQ. So why use it in a normal program when the datatype can be used in itself.

  • Avoid flickering in dynamically rendered control in Windows App

    Here is a tip shared by Kannan, a colleague of mine on how to avoid flickering when rendering controls dynamically in a Windows Application.

    Enable double buffering, so that the flickering does not happen.

    Add the following after the ‘InitializeComponent’ method.

          SetStyle(ControlStyles.UserPaint, true);
          SetStyle(ControlStyles.AllPaintingInWmPaint, true);
          SetStyle(ControlStyles.DoubleBuffer, true);
    

    That’s it.

  • DOCTYPE blues

    Yesterday I was working on the layout for a new site. I missed the DOCTYPE declaration in the HTML. The page had the main content included inside a div.

    Following is the CSS that I used.

    body {
          font-family: Georgia;
          font-size: 14px;
    }
    #main-content p{
          margin: 0;
          padding: 0;
          padding-bottom: 15px;
    }
    

    This worked perfectly and rendered well on Firefox and Chrome. But when I opened the same page in IE, the font was too large for all content inside the ‘main-content’ div. Also in order to center the page, I had used ‘margin: auto’ on the page container that encompasses all content in the body as below

    #page-container {
          width: 760px;
          margin: auto;
    }
    

    This too didn’t seem to work in IE as the content was left aligned instead of centered, but rendered ‘centered’ in Firefox and Chrome.
    Looking out for a solution on the net, I found that missing the DOCTYPE declaration was the cause for the issue. Here is the reason.

    Without the DOCTYPE declaration, the browser has rendered the page in quirks mode. Adding the DOCTYPE made the browser render the page following the rules set by the standard.

    So never forget your doctype declarations.

  • What type of programmer are you?

    While stumbling through web sites, I hit upon , Doolwind’s Game Coding Site, which presents a interesting test to find what type of programmer you are.
    My result was

    Your programmer personality type is:
    DHSB
    You’re a Doer.
    You are very quick at getting tasks done. You believe the outcome is the most important part of a task and the faster you can reach that outcome the better. After all, time is money.

    You like coding at a High level.
    The world is made up of objects and components, you should create your programs in the same way.

    You work best in a Solo situation.
    The best way to program is by yourself. There’s no communication problems, you know every part of the code allowing you to write the best programs possible.

    You are a liBeral programmer.
    Programming is a complex task and you should use white space and comments as freely as possible to help simplify the task. We’re not writing on paper anymore so we can take up as much room as we need.

    You can take the test here.

  • Design Time Support in Custom Server Controls

    Visual Studio and ASP.Net provide excellent design time support for all controls. By Design Time support, I am referring to the Values and Description provided in the Properties window when a developer places his focus on the control.

    When designing custom server controls, providing such design time support is simple.

    [Category(“Custom”)]
    [Description(“Set the Corp Image URL.”)]
    public string CorpImageURL
    {
    get
    {
    return CorpImage.ImageUrl;
    }
    set
    {
    EnsureChildControls();
    CorpImage.ImageUrl = value;
    }
    }

    Here a property CorpImageURL to set the URL for a Image control is exposed. If the ‘get’ part of the Property is not defined, then the design-time support for the property will not be enabled. So to get the design time support, it is necessary to set the ‘get’ accessor.

    The attribute ‘Category’ is used to set the category in which the property will be displayed in the properties window. So in this case, a ‘Custom’ category is defined. If the Properties window is configured to show in the ‘Categorized’ mode, this new Category, ‘Custom’ will be displayed.

    The attribute ‘Description’ is used to set the description for the property. The description of the property to instruct what the developer is supposed to provide to the property.

    The attribute ‘DefaultValue’ is used to set the default value for the property. Note: This value will be displayed only on the properties window. It will not be assigned to the property by default.

  • Starting out on a Linux Kernel

    Hi,

    I have downloaded the linux source code for version 0.0.1 from http://lxr.linux.no/.

    I just wanted to get started on reading and trying to find out what each one of the files are doing. Where do I start off. I am not quiet comfortable in semaphores or operating system stuff like that.

    Any help will be welcome.

    Thanks.

  • ASP.Net Validation controls

    ASP.Net validation controls can be used to perform both client side validations. If the page containing a asp.net validation control is rendered in a page with Javascript disabled, the asp.net runtime creates code to automatically perform the validations in the server side.

    Server side validations can be invoked using the Page.Validate() method. The status of the validation is set to the Page.IsValid property by the asp.net runtime.


    protected void Page_Load(object sender, EventArgs e)
    {
    if (Page.IsPostBack)
    {
    Page.Validate();
    }
    }

    protected void button1_Click(object sender, EventArgs e)
    {
    if (!Page.IsValid) return;
    //Do something if validation passes.
    }

    This server side validation works on all major browsers. I have tested on IE, FireFox and Netscape.