Java's Enumeration translated to C# IEnumerator -


in practice seems simple i'm getting confused it. java's enumeration hasmoreelements() , nextelement() methods related work differently c#'s ienumerator movenext() , current() properties of course. how translate this?:

//class declaration, fields constructors, unrelated code etc.  private vector atomlist = new vector();      public int getnumberbasis() {     enumeration basis = this.getbasisenumeration();     int numberbasis = 0;     while (basis.hasmoreelements()) {         object temp = basis.nextelement();         numberbasis++;     }     return numberbasis; }   public enumeration getbasisenumeration() {     return new basisenumeration(this); }      private class basisenumeration implements enumeration {      enumeration atoms;      enumeration basis;      public basisenumeration(molecule molecule) {         atoms = molecule.getatomenumeration();         basis = ((atom) atoms.nextelement()).getbasisenumeration();     }      public boolean hasmoreelements() {         return (atoms.hasmoreelements() || basis.hasmoreelements());     }      public object nextelement() {         if (basis.hasmoreelements())             return basis.nextelement();         else {             basis = ((atom) atoms.nextelement()).getbasisenumeration();             return basis.nextelement();         }     }  } 

as can see, enumration class's methods overloaded , don't think replacing hasmoreelements , nextelement movenext , current everywhere work... because basis.nextelement() calls hasmoreelements() again in if-else statement. if replace hasmoreelements movenext(), code advance twice instead of one.

you can indeed implement ienumerable yourself, needed exercises in internals of c#. you'd use either iterator method:

 ienumerable<atom> getatoms()  {     foreach(atom item in basis)     {          yield return item;     }     foreach(atom item in atoms)     {          yield return item;     }  } 

or enumerable.concat

 ienumerable<atom> getatoms()  {      return basis.concat(atoms);  } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -