user controls - VB.Net check for existing keys in a collection -
user controls - VB.Net check for existing keys in a collection -
i have class inherits collectionbase , when adding items want observe whether collection contains key going inserted. if want send warning via msgbox(). here code & i've tried
<serializable()> public class validationlist inherits collectionbase public function add(byval item validationitem) integer msgbox(me.list.contains(item)) homecoming me.list.add(item) end function default public readonly property item(byval index integer) validationitem homecoming ctype(list.item(index), validationitem) end end property public sub remove(byval index integer) me.list.removeat(index) end sub public function indexof(byval key validationitem) homecoming list.indexof(key) end function public sub addrange(byval item() validationitem) counter integer = 0 item.getlength(0) - 1 list.add(item(counter)) next end sub end class <serializable()> public class validationitem private _key validationtypes private _value string public enum validationtypes man = 0 num = 1 end enum public property value string homecoming _value end set(byval value string) _value = value end set end property public property key validationtypes homecoming _key end set(byval value validationtypes) _key = value end set end property public sub new() ' empty constructor needed serialization end sub public sub new(byval k validationtypes, byval v string) _key = k _value = v end sub end class public class textbox inherits system.windows.forms.textbox private _validation new validationlist <system.componentmodel.designerserializationvisibility(content)> public property validation validationlist homecoming _validation end set(byval value validationlist) _validation = value end set end property end class in add together method tried check whether collection has item. returns -1.
here code adds new item collection
textbox1.validation.add(new validationitem {.key = validationitem.validationtypes.man, .value = "1"})
to create contains work, you'll have implement equals/gethashcode on validationitem or implement iequatable(of t) interface:
this method determines equality using default equality comparer, defined object's implementation of iequatable(of t).equals method t (the type of values in list).
here's illustration implementation equals/gethashcode checks both, key , value:
<serializable> _ public class validationitem protected overloads function equals(other validationitem) boolean homecoming _value = other._value andalso _key = other._key end function public overrides function equals(obj object) boolean if obj nil homecoming false end if if me obj homecoming true end if if obj.gettype() isnot me.gettype() homecoming false end if homecoming equals(directcast(obj, validationitem)) end function public overrides function gethashcode() integer homecoming ((if(_value isnot nothing, _value.gethashcode(), 0)) * 397) xor cint(_key) end function ... end class you utilize use linq, here's illustration checks key:
public function add(byval item validationitem) integer if me.list.oftype(of validationitem).any(function(i) i.key = item.key) ' ' else homecoming me.list.add(item) end if end function vb.net user-controls
Comments
Post a Comment