Results 1 to 6 of 6

Thread: Help with foreach [Resoloved]

  1. #1

    Thread Starter
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Resolved Help with foreach [Resoloved]

    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.

    Code:
    /*
    * 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);
    }
    
    
    }
    }
    }
    Thanks

  2. #2
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    I think what's happening here is, when you add the new object to your list, you are not adding all the data, per se, you are only adding a pointer to the object. So let's say the object resides in memory location 103. Each time you 'Add', you are really adding a bunch of '103s' to your list. Now when you change elements of this object residing in location 103, all your pointers are just pointing to the same object. To resolve this you would have to make sure you are creating a new object before you add it to the list. There are several ways you can do this. I notice in your code you have a couple lines //p = new Person() commented out. If you uncomment them, does it then work correctly?

    cudabean

  3. #3

    Thread Starter
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    It works if i uncomment the and creat a new instance of Person each time but is it possible to use a simmalar method with out making a new object.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    No. A Cudabean explained Arraylist is holding memory pointers not actual values. Using new assigns a new memory address.

    This isnt VB6.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    Yes I know it's not VB6 but it's a hard mindset to shift.
    have found a method of doing what i wished by changing the add method of my collection.

    Code:
          public virtual void Add(Person p )
            {
                Person personToAdd = new Person();
                personToAdd.Age=p.Age;
                personToAdd.Name=p.Name;
                           
                    
                personArray.Add(personToAdd);
                
                
            }
    Thanks all!!

  6. #6
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    It works if i uncomment the and creat a new instance of Person each time but is it possible to use a simmalar method with out making a new object.
    Yes, it's entirely possible. I think YOU would have to write the Add method. Your add method would have to issue the 'new' to allocate the space for your object. (In fact, I see you have done this in the previous post) However, I think it gets you in a good mindset to NOT do this, because most of the built-in functions in C# (and Java as well) are designed where you are in charge of instantiating your objects. Plus, if methods started appearing like the one you have just written, programmers would have to start becoming more vigilant and check if the particular Method requires objects to be pre-instantiated, or if it does it for you. This causes confusion and makes the programming more error-prone.

    cudabean
    Last edited by Cudabean; Sep 24th, 2004 at 12:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width