ArrayList.Add - unexpected behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam

    #1

    ArrayList.Add - unexpected behavior

    Hi all,

    The output of the code below is:
    This is Message 2
    This is Message 2

    But I expected:
    This is Message 1
    This is Message 2

    Can anyone help me understand what I am doing wrong? Thanks!


    using System;
    using System.Collecti ons;

    namespace test
    {
    public class Message
    {
    private string text;
    public string Text
    {
    get{return text;}
    set{text=value; }
    }
    }
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    ArrayList Messagebox = new ArrayList();

    Message oMessage=new Message();
    oMessage.Text=" This is Message 1";
    Messagebox.Add( oMessage);
    oMessage.Text=" This is Message 2";
    Messagebox.Add( oMessage);

    Message mymess=(Message )Messagebox[0];
    Console.WriteLi ne(mymess.Text) ;
    Message mymess2=(Messag e)Messagebox[1];
    Console.WriteLi ne(mymess.Text) ;
    }
    }
    }


  • Adam

    #2
    Re: ArrayList.Add - unexpected behavior

    Correction, here's my code:

    using System;
    using System.Collecti ons;

    namespace test
    {
    public class Message
    {
    private string text;
    public string Text
    {
    get{return text;}
    set{text=value; }
    }
    }
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    ArrayList Messagebox = new ArrayList();

    Message oMessage=new Message();
    oMessage.Text=" This is Message 1";
    Messagebox.Add( oMessage);
    oMessage.Text=" This is Message 2";
    Messagebox.Add( oMessage);

    Message mymess=(Message )Messagebox[0];
    Console.WriteLi ne(mymess.Text) ;
    mymess=(Message )Messagebox[1];
    Console.WriteLi ne(mymess.Text) ;
    }
    }
    }


    "Adam" <nospam@comcast .net> wrote in message
    news:SbUxb.3418 30$HS4.2919597@ attbi_s01...[color=blue]
    > Hi all,
    >
    > The output of the code below is:
    > This is Message 2
    > This is Message 2
    >
    > But I expected:
    > This is Message 1
    > This is Message 2
    >
    > Can anyone help me understand what I am doing wrong? Thanks!
    >
    >
    > using System;
    > using System.Collecti ons;
    >
    > namespace test
    > {
    > public class Message
    > {
    > private string text;
    > public string Text
    > {
    > get{return text;}
    > set{text=value; }
    > }
    > }
    > class Class1
    > {
    > [STAThread]
    > static void Main(string[] args)
    > {
    > ArrayList Messagebox = new ArrayList();
    >
    > Message oMessage=new Message();
    > oMessage.Text=" This is Message 1";
    > Messagebox.Add( oMessage);
    > oMessage.Text=" This is Message 2";
    > Messagebox.Add( oMessage);
    >
    > Message mymess=(Message )Messagebox[0];
    > Console.WriteLi ne(mymess.Text) ;
    > Message mymess2=(Messag e)Messagebox[1];
    > Console.WriteLi ne(mymess.Text) ;
    > }
    > }
    > }
    >
    >[/color]


    Comment

    • Frank Oquendo

      #3
      Re: ArrayList.Add - unexpected behavior

      Adam wrote:
      [color=blue]
      > Message oMessage=new Message();
      > oMessage.Text=" This is Message 1";
      > Messagebox.Add( oMessage);
      > oMessage.Text=" This is Message 2";
      > Messagebox.Add( oMessage);[/color]

      The problem is right here. Instead of creating a new instance and
      setting its Text property for the second call to ArrayList.Add, you
      modified the Text of the first instance and added it to your ArrayList
      again.

      So now you've got two references to a single Message object whose Text
      property is set to "This is Message 2".

      --
      There are 10 kinds of people. Those who understand binary and those who
      don't.


      (Pull the pin to reply)


      Comment

      • Adam

        #4
        Re: ArrayList.Add - unexpected behavior

        Thanks Frank for the information.

        I may need to add hundreds of messages at a time into this ArrayList. Is
        there an efficient way to add the messages to the ArrayList in series,
        without having to do something like this:?

        Message MessageA=new Message();
        MessageA.Text=" First message";
        Messagebox.Add( MessageA);

        Message MessageB=new Message();
        MessageB.Text=" Second message";
        Messagebox.Add( MessageB);

        Message MessageC=new Message();
        MessageC.Text=" Third message";
        Messagebox.Add( MessageC);

        Message MessageD=new Message();
        MessageD.Text=" Fourth message";
        Messagebox.Add( MessageD);

        etc. etc.

        Thanks!



        The reason I ask is that say I need to add 6 or 7 messages in a row into

        "Frank Oquendo" <frankopin@acad x.com> wrote in message
        news:%23PKRaqit DHA.2136@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > Adam wrote:
        >[color=green]
        > > Message oMessage=new Message();
        > > oMessage.Text=" This is Message 1";
        > > Messagebox.Add( oMessage);
        > > oMessage.Text=" This is Message 2";
        > > Messagebox.Add( oMessage);[/color]
        >
        > The problem is right here. Instead of creating a new instance and
        > setting its Text property for the second call to ArrayList.Add, you
        > modified the Text of the first instance and added it to your ArrayList
        > again.
        >
        > So now you've got two references to a single Message object whose Text
        > property is set to "This is Message 2".
        >
        > --
        > There are 10 kinds of people. Those who understand binary and those who
        > don't.
        >
        > http://code.acadx.com
        > (Pull the pin to reply)
        >
        >[/color]


        Comment

        • Michael A. Covington

          #5
          Re: ArrayList.Add - unexpected behavior


          "Adam" <nospam@comcast .net> wrote in message
          news:cPUxb.3471 51$Fm2.351286@a ttbi_s04...[color=blue]
          > Thanks Frank for the information.
          >
          > I may need to add hundreds of messages at a time into this ArrayList. Is
          > there an efficient way to add the messages to the ArrayList in series,
          > without having to do something like this:?
          >
          > Message MessageA=new Message();
          > MessageA.Text=" First message";
          > Messagebox.Add( MessageA);[/color]

          How about defining a constructor for Message, and doing this?

          Messagebox.Add( new Message("First message"))
          Messagebox.Add( new Message("Second message"))

          and so on...


          Comment

          Working...