vb.net - How to Use A Different GetEnumerator() in For Each Statement -


my custom collection example implements ienumerable(of long) , idictionary(of string, long).

when enumerate through collection for each, values of type keyvaluepair(of string, long), want values of type long.

the 3 getenumerator's only differ in return type. not overloading.

module main     sub main()         dim examples new example          each e in examples             ' e of type keyvaluepair(of string, long)             ' want e of type long         next     end sub end module  public class example     implements ienumerable(of long), idictionary(of string, long)      private property thekeyedcollection keyedcollection(of string, long)      public function getenumerator() ienumerator(of keyvaluepair(of string, long)) implements ienumerable(of keyvaluepair(of string, long)).getenumerator         return thekeyedcollection.getenumerator()     end function      public function getenumerator1() ienumerator(of long) implements ienumerable(of long).getenumerator         return thekeyedcollection.getenumerator()     end function      public function getenumerator2() ienumerator implements ienumerable.getenumerator         return thekeyedcollection.getenumerator()     end function       '>>>>>>>>>>>>>>>>>>>>>>>>>>>> ignore these other unnecessary functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<     private sub add(item keyvaluepair(of string, long)) implements icollection(of keyvaluepair(of string, long)).add     end sub     private sub clear() implements icollection(of keyvaluepair(of string, long)).clear     end sub     private function contains(item keyvaluepair(of string, long)) boolean implements icollection(of keyvaluepair(of string, long)).contains         throw new exception     end function     private sub copyto(array() keyvaluepair(of string, long), arrayindex integer) implements icollection(of keyvaluepair(of string, long)).copyto     end sub     private readonly property count integer implements icollection(of keyvaluepair(of string, long)).count                     throw new exception         end     end property     private readonly property isreadonly boolean implements icollection(of keyvaluepair(of string, long)).isreadonly                     throw new exception         end     end property     private function remove(item keyvaluepair(of string, long)) boolean implements icollection(of keyvaluepair(of string, long)).remove         throw new exception     end function     private sub add1(key string, value long) implements idictionary(of string, long).add     end sub     private function containskey(key string) boolean implements idictionary(of string, long).containskey     end function     '>>>>>>>>>>>>>>>>>>>>>>>>>>>> ignore these other unnecessary functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<     private property item(key string) long implements idictionary(of string, long).item                     throw new exception         end         set(value long)         end set     end property     private readonly property keys icollection(of string) implements idictionary(of string, long).keys                     throw new exception         end     end property     private function remove1(key string) boolean implements idictionary(of string, long).remove         throw new exception     end function     private function trygetvalue(key string, byref value long) boolean implements idictionary(of string, long).trygetvalue         throw new exception     end function     private readonly property values icollection(of long) implements idictionary(of string, long).values                     throw new exception         end     end property     '>>>>>>>>>>>>>>>>>>>>>>>>>>>> ignore these other unnecessary functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< end class 

i tried setting getenumerator() , getenumerator2() private, produced error: 'for each' on type 'example.example' ambiguous because type implements multiple instantiations of 'system.collections.generic.ienumerable(of long)'.

how pick getenumerator() for each uses?

change name of function want default enumerator getenumerator(). not getenumerator1().

if don't plan use other 2 versions of getenumerator, can set them private, still have there satisfy given interface.

if want use 1 of getenumerator()s not default, i.e. getenumerator1(), need force for each infer this:

for each e in directcast(example, ienumerable(of string))     ' e of type string next 

Comments