Wednesday, March 31, 2010

Power of lambda

Lambda is like sudo make me sandwich:  ^_^
string prefix = "tm";

    .Where(cx => cx.Name.Substring(0, 2) == prefix)
    .ForEach(cx => cx.Visible = false); // do something here in ForEach

The above code reads as: prepare me a list of checkbox which starts with letters tm, and i'll do something on those checkboxes. You can concentrate on things you want to do.

I'll make it myself(without lambda):
string prefix = "tm";

foreach(Control cx in this.GetAllControls())
    if (cx is CheckBox && cx.Name.Substring(0, 2) == prefix)
        // payload here, er.. do something here
        (cx as CheckBox).Visible = false; 
    // who can be possibly sure that prefix wasn't altered here
    // prefix = "ax";

The above code reads as: i'm looking at all controls, check if control is checkbox and starts with letters tm, then i'll do something on that checkbox. And because it is loop, programmers tend to re-read the loop header's condition if all corner cases are covered.

Suppress Crystal Report Viewer's Main Report Tab

public static void SuppressReportTabs(
    this CrystalDecisions.Windows.Forms.CrystalReportViewer crv)
    foreach (System.Windows.Forms.Control control in crv.Controls)
        if (control is CrystalDecisions.Windows.Forms.PageView)
            var tab = 
                (control as CrystalDecisions.Windows.Forms.PageView)
                .Controls[0] as System.Windows.Forms.TabControl;

            tab.ItemSize = new System.Drawing.Size(0, 1);
            tab.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
            tab.Appearance = System.Windows.Forms.TabAppearance.Buttons;

Sunday, March 28, 2010

Updating a Data Source with a Dataset

my answer on stackoverflow question Updating a Data Source with a Dataset:

public bool SecureUpdateDataSet(DataSet delta)

     string connStr = WebConfigurationManager.ConnectionStrings["Employees"].ConnectionString;

     using(var conn = new SqlConnection(connStr))

        string sql = "select * from tab1 where 1 = 0";

        var da = new SqlDataAdapter(sql, conn);

        var builder = new SqlCommandBuilder(ad);

        da.InsertCommand = builder.GetInsertCommand();
        da.UpdateCommand = builder.GetUpdateCommand();
        da.DeleteCommand = builder.GetDeleteCommand();


        return true;

    return false;

Friday, March 26, 2010

Hello World

using System;
namespace Craft
    public class Mate
        public static void Main() 
            Console.WriteLine("Hello World!");