472,378 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 2199
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Omer | last post by:
Hi All, We are trying to invoke EJB methods on simple stateless session beans from an Applet running remotely. One of our methods needs to return an ArrayList of Serializable objects. We...
1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
3
by: Kail | last post by:
Hello all, today i found a very strange situation of ArrayList - at least for me. let's take 3 elementes that were put into list variable (list.add(point 1 - 3)): Point(1,1); Point(2,2);...
1
by: Sylvain | last post by:
Hi, I'm encountering a very simple issue with ArrayList constructor and AddRange() method overriding. I'm defining a class that extends ArrayList and contains one overriden method:...
8
by: Maileen | last post by:
Hi, I would like to have something like an array (something like a collection) in order to store the following things : company name1, new1, old1, in1, out1 company name2, new2, old2, in2,...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
2
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Running the script below prints 'Unexpected': ----------------------------- DECLARE @String AS varchar(1) SELECT @String = 'z' IF @String LIKE ''
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.