i have function scrambled comparison
public static int scrambledequals<tkey, t>( idictionary<tkey, t> list1, idictionary<tkey, t> list2) tkey : icomparable t : icompareashtml or icomparable // compilation failure on line { // ... } public static int scrambledequals<tkey, t>( idictionary<tkey, t> list1, idictionary<tkey, t> list2) tkey : icomparable t : icompareashtml {} // works! the tkey has icomparable, value type t can icomparable or implementing below interface
public interface icompareashtml { // compare current "this" object "obj" // , persist difference in html somewhere // return number of differences. int compareashtml(object obj); } how can update line make compilation pass (not delete it, not use 1 of interfaces)?
or better, can read code below understand situation. need use below function compare collection of string, icomparable; or compare collection of hug class, sales, implementing icompareashtml not icomparable. (because many properties make class hard implement compareto function - not able give out "one" number represent "direction" , "distance" between 2 instances.)
public static int scrambledequals<tkey, t>( idictionary<tkey, t> list1, idictionary<tkey, t> list2) tkey : icomparable // t : icompareashtml or icomparable // commented out above line make compilation pass { int ndiff = 0; list<tkey> bothkeys = list1.keys.union<tkey>(list2.keys).tolist(); bothkeys.sort(); foreach (tkey key in bothkeys) { // code omitted - not related question { // key exist in both lists. object o1 = list1[key]; object o2 = list2[key]; if (o1 icomparable && o2 icomparable) { icomparable v1 = (icomparable)o1; icomparable v2 = (icomparable)o2; if (0 != v1.compareto(v2)) { ndiff++; // save difference in html } } else if (o1 icompareashtml && o2 icompareashtml) { icompareashtml v1 = (icompareashtml)o1; icompareashtml v2 = (icompareashtml)o2; // save difference ndiff += v1.compareashtml(v2); } else { // if can use keyword // don't need exception throw new exception(@"error: program error - value type neither icomparable nor icompareashtml."); } } } } return ndiff; } all suggestion / feedback / comments welcome!
this not supported framework. however, need implies there missing common interface both icomparable , icompareashtml should implement constrain to, instead.
the bad news here icomparable part of framework, meaning can't change implement missing interface. news icomparable pretty simple... perhaps can be missing interface.
icompareashtml isn't part of framework, , not mentioned in docs product can search via google, meaning it's interface can change. if that's true, make icompareashtml implement icomparable , constrain icomparable, , meet conditions
Comments
Post a Comment