Now I come from VB6 and one of the fetures I loved was a collection class now I can do it in C# please code below but what i wish to know is why i have to create a new instance of the class before i can add it to my collection (ArrayList) i never had to do this in VB6, what seems to happen is the last entered object overwrites all other data.
ThanksCode:/* * Created by SharpDevelop. * User: BombDrop * Date: 13/09/2004 * Time: 12:57 * */ using System; using System.Collections; namespace DefaultName { public class Person { private int age; private string name; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public Person() { } } public class Persons : object, IEnumerable, IEnumerator { private ArrayList arr = new ArrayList(); private int position=-1; public Persons() { } public virtual object Current { get { return arr[position]; } } public virtual IEnumerator GetEnumerator() { return (IEnumerator)this; } public virtual void Reset() { this.position=-1; } public virtual bool MoveNext() { this.position++; if (this.position <arr.Count) { return true; } else { return false; } } public virtual void Add(Person p ) { arr.Add(p); } } class MainClass { public static void Main(string[] args) { Person p = new Person(); Persons ps = new Persons(); p.Age=28; p.Name="Bomb"; ps.Add(p); //p=new Person(); p.Age=29; p.Name="Drop"; ps.Add(p); //p=new Person(); p.Age=30; p.Name="BombDrop"; ps.Add(p); try { foreach(Person me in ps) { Console.WriteLine(me.Name + " " + me.Age.ToString()); } } catch(System.Exception ex) { Console.WriteLine(ex.Message); } } } }
![]()
![]()
![]()




Reply With Quote