coding snippet
Update Statement with Other Table
by calendarw on Apr.23, 2009, under coding snippet
呢排為設置 Testing 場而預備緊 d SQL statement, 有部份 Data 得 detail table, 但使用時要整返個 master 比佢, 所以就要使用 update statement 黎 link 返唔同 table 既 key, 以下係一個例子:
update tblModel set series_pkey = tblSeries.pkey from tblModel, tblSeries where tblSeries.full_name = 'A Series' and tblModel.full_name like 'A %';
Understanding Exception
by calendarw on Mar.29, 2009, under coding snippet, diary
Exception – 從來都被初哥覺得係個麻煩既來源, 但其實佢只係一個提醒工具, 話比大家知那裡有問題.
好多課程, 以及書本都有教點處理 Exception, 但如何正確使用 Exception 就好似得 Pragmatic Programmer 一書內有提及.
通常我使用 Exception 既地方會有以下幾個, 作用係提醒返 d 唔正常既動作, 因為唔 throw 既話, 使用者 (包括自己) 都會唔知或者唔記得:
1. Design By Contract
主要用 Exception 黎 validate 所有 input 或 pre-condition, 令佢有個 Valid State 比佢 perform action
public void Cancel()
{
if (item.IsLoaning)
throw new InvalidOperationException("Please return the loan before cancel");
}
2. Supporting of Share Module
public static IDbDataAdapter CreateDbDataAdapter(IDbCommand command)
{
IDbDataAdapter adapter = null;
if (command is SqlCommand)
{
adapter = new SqlDataAdapter((SqlCommand)command);
}
if (command is OleDbCommand)
{
adapter = new OleDbDataAdapter((OleDbCommand)command);
}
if (command is OdbcCommand)
{
adapter = new OdbcDataAdapter((OdbcCommand)command);
}
if (adapter != null)
{
return adapter;
}
throw new NotSupportedException();
}
3. Supporting on different framework
public void NewFeature()
{
#if !USING_NEW_FRAMEWORK
throw new NotSupportedException();
#else
doSomething();
#endif
}
4. 又或者係未做完的工作, 不過好多時都會寫 // TODO
public void DoSomething()
{
throw new NotImplementedException();
}
C# 3.0 初試!!
by calendarw on Mar.13, 2009, under coding snippet
用左 VS 2008 其實都幾個月, 但因公司目標係 .net 2.0 既關係, 技術始終都仲停留緊係 2.0 度, 近日開始習慣緊 C# 3.0 既 syntax, 有幾個新 feature 係 2.0 platform 都幾好用.
Object initializers
呢個係一個新既 contractor syntax, 可以好方便咁創建同設定 Value. 但如果個 Properties 係 private set; 既話就用唔到啦!!
Customer c1 = new Customer { Name="James" };
Customer c2 = new Customer { Name="Tom" };
Lambda expressions
Lambda 好好用!! 簡單, 快捷!!
string name = "James"; IList<Customer> result = list.FindAll(x => name.Equals(x.Name));
Automatic properties
呢個可以簡化到部份 properties, 但如果有 business logic 既話就唔係太啱用
public decimal UnitPrice
{
get;
private set;
}
Working with Dock
by calendarw on Feb.04, 2009, under coding snippet
近半年開始著重地寫 Windows Application (以前以 web 同 DLL 為主), 寫左咁耐都無用過 Control.Dock 呢個屬性, 今日第一次用, 就出左好多次序上既問題, 不過在網上找到以下既解決方案, 當解決了次序既問題後, Dock 其實係幾好用!!~~
private void RemoveBtn(Button btn1)
{
if(this.Controls.Contains(btn1))
this.Controls.Remove(btn1);
}
private void SetDockProperity(Button btn1, DockStyle dockStyle)
{
btn1.Dock = dockStyle;
}
private void DockTop_Click(object sender, System.EventArgs e)
{
RemoveBtn(this.button1);
RemoveBtn(this.button2);
RemoveBtn(this.button3);
SetDockProperity(this.button1, System.Windows.Forms.DockStyle.Top);
SetDockProperity(this.button2, System.Windows.Forms.DockStyle.Top);
SetDockProperity(this.button3, System.Windows.Forms.DockStyle.Top);
this.Controls.Add(this.button3); // 注意順序, 後 Add 的最上面
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
}
private void DockBottom_Click(object sender, System.EventArgs e)
{
RemoveBtn(this.button1);
RemoveBtn(this.button2);
RemoveBtn(this.button3);
SetDockProperity(this.button1, System.Windows.Forms.DockStyle.Bottom);
SetDockProperity(this.button2, System.Windows.Forms.DockStyle.Bottom);
SetDockProperity(this.button3, System.Windows.Forms.DockStyle.Bottom);
this.Controls.Add(this.button3); // 注意順序, 後 Add 的最下面
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
}
轉載自: 藍色小鋪
Implement State of View
by calendarw on Aug.03, 2008, under coding snippet
平時寫介面(GUI), 通常會寫一個 method 叫 StateChange 黎處理所有介面上既所有 visible 同 enable 既動作. 而自己會因介面既設計而定義一個 private enum 既 State, 所有 state 由一個 method 做晒.
private enum State
{
Add,
Update
}
private void StateChange(State eState)
{
if (eState == State .Add)
{
txtId.Visible = true;
lblId.Visible = false;
lblMode.Text = "[Add New]";
}
if (eState == State .Update)
{
txtId.Visible = false;
lblId.Visible = true;
lblMode.Text = "[Update]";
}
}
而我自己寫過既 State 都有幾隻:
同一版有個所有 Item 既 List 而 Edit box 得 Add 同 Update
private enum State
{
Add,
Update
}
一版得主要係用黎 Insert 既:
private enum State
{
WaitingSave,
WaitingConfirm // for double confirm
}
同埋得睇同改既:
private enum State
{
Normal,
Edit // for add and update
}
仲有好多其他既 State 寫過, 不過都太過多同太專門, 所以應該係按照實際用途而加.
網上曾經見過有人用 State object 同 State Pattern 黎做, 對我而言好似用係 GUI 方面未必真係有咁既需要, 但對一個 domain object 而言就最好有 State object, 咁樣會對個 design 好好多.
