469,903 Members | 1,542 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

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.Collections;

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.WriteLine(mymess.Text);
Message mymess2=(Message)Messagebox[1];
Console.WriteLine(mymess.Text);
}
}
}
Nov 15 '05 #1
4 2137
Correction, here's my code:

using System;
using System.Collections;

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.WriteLine(mymess.Text);
mymess=(Message)Messagebox[1];
Console.WriteLine(mymess.Text);
}
}
}
"Adam" <no****@comcast.net> wrote in message
news:SbUxb.341830$HS4.2919597@attbi_s01...
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.Collections;

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.WriteLine(mymess.Text);
Message mymess2=(Message)Messagebox[1];
Console.WriteLine(mymess.Text);
}
}
}

Nov 15 '05 #2
Adam wrote:
Message oMessage=new Message();
oMessage.Text="This is Message 1";
Messagebox.Add(oMessage);
oMessage.Text="This is Message 2";
Messagebox.Add(oMessage);


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)
Nov 15 '05 #3
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" <fr*******@acadx.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Adam wrote:
Message oMessage=new Message();
oMessage.Text="This is Message 1";
Messagebox.Add(oMessage);
oMessage.Text="This is Message 2";
Messagebox.Add(oMessage);


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)

Nov 15 '05 #4

"Adam" <no****@comcast.net> wrote in message
news:cPUxb.347151$Fm2.351286@attbi_s04...
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);


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...
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Omer | last post: by
1 post views Thread by Jamus Sprinson | last post: by
8 posts views Thread by Maileen | last post: by
9 posts views Thread by Jeff Louie | last post: by
2 posts views Thread by Dimitri Furman | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.