この質問には2つの部分があります。イベントを1つのハンドラにまとめると効率的ですか?
- は、そのオブジェクトのイベントをオブジェクトごとに1つのイベント固有の機能を持っているか、組み合わせ方が良いですし、もしそうなら、
- は、送信者のオブジェクトを定義し、効率的でいただきましたが、更新されて見つけるために比較するためのハッシュコードを使用しますか?
以下のコード例。
マイイベント機能:
private void GeneralEventHandler(object sender, EventArgs e){
var senderHash = sender.GetHashCode();
if (senderHash == tbDatabase.GetHashCode())
Settings.DB.Default.Database = tbDatabase.Text;
else if (senderHash == tbSchema.GetHashCode())
Settings.DB.Default.Schema = tbSchema.Text;
}
マイオブジェクト定義:
// tbDatabase
/
this.tbDatabase.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.tbDatabase.Location = new System.Drawing.Point(62, 3);
this.tbDatabase.Name = "tbDatabase";
this.tbDatabase.Size = new System.Drawing.Size(210, 20);
this.tbDatabase.TabIndex = 0;
this.tbDatabase.LostFocus += new System.EventHandler(this.GeneralEventHandler);
//
// tbSchema
//
this.tbSchema.Location = new System.Drawing.Point(55, 3);
this.tbSchema.Name = "tbSchema";
this.tbSchema.Size = new System.Drawing.Size(217, 20);
this.tbSchema.TabIndex = 1;
this.tbSchema.LostFocus += new System.EventHandler(this.GeneralEventHandler);
私は主にだけその場でユーザー設定を更新するためにそれを使用しています。終了時に設定ファイルを保存します。
読みやすさ/保守性の観点から、もし彼らが異なることをするならば、彼ら自身のハンドラを与えます。 –
異なるオブジェクトは同じハッシュコードを持つことができます。ハッシュコードをコード内の一意の識別子として使用できると想定していませんか? –
また、なぜハッシュ部分を気にするのですか? sender == yourObjectかどうかを確認してください。 –