c# - ErrorProvider with custom GetHashCode -
i have form responsible of creating (and saving) new patients. on form using errorprovider show error icons on invalid fields (in case "lastname"). so, usual => errorprovider.datasource = patient;
everything works fine when model uses default gethashcode(). when try override method using custom hash code (i want use model iset collections) control not work properly. now, understand custom hash codes should used immutable objects. point is, how can fill fields of these objects if errorprovider behaviour relays on gethashcode work properly? necessary implement dirty mechanism switches between default hash code (during object initialization) , custom hash?
code sample:
public class patient : idataerrorinfo, inotifypropertychanged { public string lastname; public virtual string lastname { { return lastname; } set { if (lastname == value) return; lastname = value; notifypropertychanged("lastname"); } } #region idataerrorinfo members string idataerrorinfo.error { { return null; } } string idataerrorinfo.this[string propertyname] { { return this.getvalidationerror(propertyname); } } #endregion // idataerrorinfo members protected string getvalidationerror(string propertyname) { if (validatedproperties.indexof(propertyname) < 0) return null; string error = null; switch (propertyname) { case "lastname": if (lastname == null) error = "null"; break; default: break; } return error; } public virtual event propertychangedeventhandler propertychanged; protected void notifypropertychanged(string propertyname) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(propertyname)); } public override int gethashcode() { unchecked { int result = 17; result = 23 * result + ((lastname != null) ? lastname.gethashcode() : 0); return result; } } }
each field used in gethashcode
function must immutable. not recommend implement 2 versions of gethashcode
, because should persistent , repeatable. know 1 possible way how solve problem. if know object changed, can delete set before editing operation , add again set when modifications done. in case can skip overriding of gethashcode
, use sortedset specified comparer implements icomparer interface.
update
normally, recommend use hashset
if don't need sorted set result. sortedset
employs binary search tree , seems doesn't use gethashcode
function. sortedset
bit slower hashset
. sortedset
performance o(log n), because has find space inserting element in sorted set. hashset takes o(1).
icomparer
helps find whether 2 objects equal (there no need call equals
) or tells of them less or greater other. wrote bit of code testing of sortedset functionality.
code
public class foo { public foo(string something) { = something; } public string { set; get; } } public class bysomething : icomparer<foo> { private readonly caseinsensitivecomparer _comparer = new caseinsensitivecomparer(); public int compare(foo x, foo y) { return _comparer.compare(x.something, y.something); } }
test
[testmethod] public void sortedsettest() { var first = new foo("doe"); var second = new foo("floyd"); var third = new foo("floyd"); var set = new sortedset<foo>(new bysomething()); set.add(first); set.add(second); set.add(third); assert.areequal(set.count, 2); }
Comments
Post a Comment