2008年3月31日星期一

C# - Worth To Use Extension Methods

對於經常在VisualBasic和C#之間往來的我來說,會明白到C# Strong Type的好處,但同時亦覺得VB語法寬鬆的好處.
因為在C#中要經常做轉型(Convert.ToString / .ToString etc) , 在VB中,Object可以和Integer相加 , 在C#的話, 就肯定是出現Error.

這一陣子寫Silverlight的時間,經常有很多object type出現,由於是coordinate,所以必須轉換成Double.

就以下面的Code為例,ImgBall的GetValue是object type,不能就這樣做operation,所以必須用Convert.ToDouble轉型,但單單一小節Code,已經出現3次:
if (Convert.ToDouble(ImgBall.GetValue(Canvas.LeftProperty)) < Convert.ToDouble(ImgPaddle.GetValue(Canvas.LeftProperty)) + 5)
{
ImgBall.SetValue(Canvas.LeftProperty, Convert.ToDouble(ImgPaddle.GetValue(Canvas.LeftProperty)) + 5);
}

Extension Methods的出現可大大降低程式碼的長度,效能雖然沒有得益,但增加程式碼的可讀性.
以下我就示範一下如何透過Extension Methods令整個程序更美觀.

建立一個Class檔案 (ext.cs) ,在Namespace低下加入:
public static class ext
{
/// <summary>
///  Get Dependency Object and Convert to Double.
/// </summary>
public static Double GetDouble(this Image img, DependencyProperty property)
{
return Convert.ToDouble(img.GetValue(property));
}

/// <summary>
///  Get Canvas.LeftProperty From Image.
/// </summary>
public static Double GetCanvasLeft(this Image image)
{
return GetDouble(image, Canvas.LeftProperty);
}
/// <summary>
///  Get Canvas.TopProperty From Image.
/// </summary>
public static Double GetCanvasTop(this Image image)
{
return GetDouble(image, Canvas.TopProperty);
}
}

以上程序就是把Property引入,並進行類型轉換,要注意緊記加入"static"的Modifier.

回到開始的部份,我們已經可以在Image Member中找到GetCanvasLeft 和GetCanvasTop,直接引用就已經可以得到Double類型的數值了,是不是簡短很多呢~
if (ImgBall.GetCanvasLeft()  < ImgPaddle.GetCanvasLeft() + 5)
{
ImgBall.SetValue(Canvas.LeftProperty, ImgPaddle.GetCanvasLeft() + 5);
}

其實某程度上Extension Methods和傳統的Function相似,都是可以引入Parameter , return數值等等, 但Extension Methods是指定在某一個類別下衍生出功能,當實際使用時就會依據類別而出現於Member List中,亦算是符合一般程式概念.

WordPress 2.5 Release!!!!

呢次2.5版本是大更新,由v2.3x直接跳過v2.4出v2.5已可見.

主要新加:
Dashboard Widgets (太好了,Login後,唔洗再睇到無用既資訊)
Multi-file upload with progress bar (呢個都唔錯喇,等左咁耐終於有既Function)
Few-click plugin upgrades (呢個我覺得有所保留,因為好睇Plug-in本身,好似SEO All-In-One,我見過好多人用One Click Update而失敗)
Friendlier visual post editor (轉用TinyMCE 3.0 ,但不知功能是不是又係大幅減化)
Built-in galleries (效果還可以,不過NextGen(即係我用緊呢個)更好,Demo按此: WordPress 2.5 Build-in Gallery

還有很多細微更新,可以去官網睇睇:
http://wordpress.org/development/2008/03/wordpress-25-brecker/


2008年3月29日星期六

Adobe Photoshop Express

我試用過幾下,與同類型既Webware比較,我覺得FotoFlexerPicnik比較好用,不過自己對畫圖冇任何興趣,所以都沒有太試真。

Adobe Photoshop Express :
http://www.photoshop.com/express/

2008年3月20日星期四

原來而家RSS Reader已經咁先進

自從果時用過Bloglines幾個星期後,都冇再用類似既o野,
琴日Check我Blog既Stats,發現好多人都用Google Reader,我又試下用,覺得幾好用。

起碼介面簡潔,好過Bloglines。但係我就驚見Google Reader有一樣好神奇既功能,就係當標題拉到最尾一個時,佢會自動下載其後的標題。

我一直以為RSS的文章數目是限制於XML File。
例如我用Sinablog果時,Sina最多只可以Feed 10個文章,就算你set多過10個,就唔會出現第11個文章。
所以就唔明白Google Reader點樣做到拎之後的文章,有冇網友可以簡單講解一下?