473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

wierd behavior

Tried the following c# code

static void Main(string[] args)

{

ArrayList list = new ArrayList();

int i = 10;

string s = "Test";

list.Add(i);

list.Add(s);

// run it without/with commenting the following 2 lines

s = "hello";

i = 90;

Console.WriteLine(string.Format("{0}, {1}\n", i, s));

Console.WriteLine(list.Contains(i));

Console.ReadKey();

}

It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?

muthu

http://techblog.muthuka.com/


Dec 11 '06 #1
4 1499
updated code:
ArrayList list = new ArrayList();

int i = 10;

string s = "Test";

list.Add(i);

list.Add(s);

// run it without/with commenting the following 2 lines

s = "hello";

i = 90;

Console.WriteLine(string.Format("{0}, {1}\n", i, s));

Console.WriteLine(list.Contains(i) + "\n");

IEnumerator en = list.GetEnumerator();

while (en.MoveNext())

{

Console.WriteLine(en.Current);

}

Console.ReadKey();

"Muthu Arumugam" <mu*******@hotmail.comwrote in message
news:uN**************@TK2MSFTNGP04.phx.gbl...
Tried the following c# code

static void Main(string[] args)

{

ArrayList list = new ArrayList();

int i = 10;

string s = "Test";

list.Add(i);

list.Add(s);

// run it without/with commenting the following 2 lines

s = "hello";

i = 90;

Console.WriteLine(string.Format("{0}, {1}\n", i, s));

Console.WriteLine(list.Contains(i));

Console.ReadKey();

}

It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?

muthu

http://techblog.muthuka.com/


Dec 11 '06 #2
Muthu Arumugam <mu*******@hotmail.comwrote:

<snip>
It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?
No, it's not a bug. The ArrayList doesn't contain the variables, it
contains the values the variables had when you called Add.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 11 '06 #3
"Muthu Arumugam" <mu*******@hotmail.comwrote in message
news:uN**************@TK2MSFTNGP04.phx.gbl...
[...]
It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?
It has perfectly normal behavior. Not weird at all.

When you add i to the ArrayList, you are not adding a reference to the
variable i. You are adding a reference to a newly created object containing
the value 10.

When you add s to the ArrayList, you are not adding a reference to the
variable s. You are adding the same reference to "Test" that s contains at
that moment.

When you change i, you don't change the value of the object that the
ArrayList references. You just change i. Likewise, when you change s, you
don't change the reference that the ArrayList references. You just change
the reference that s references.

If you want the content of the ArrayList to change when you change something
else, you need to add to ArrayList a reference to some mutable object that
you have access to.

I haven't looked too closely at how boxing works, but it's possible that if
you kept the Object reference created when you add i to the ArrayList:

Object obj;

obj = i;
list.Add(obj);

you could then change the value contained by obj, which would also be
reflected in the reference to obj that the ArrayList has. I don't recall
whether boxed data is mutable or not.

As far as the string goes, you could keep the reference to "Test", but since
string objects are immutable, there's no way to modify the data that
reference refers to. You would need some different kind of object that
essentially contains a string, but which is mutable (maybe StringBuilder
would work for you in that case).

Pete
Dec 11 '06 #4
Great, now i understand. sorry, it's my mistake

thanks

muthu

"Muthu Arumugam" <mu*******@hotmail.comwrote in message
news:uN**************@TK2MSFTNGP04.phx.gbl...
Tried the following c# code

static void Main(string[] args)

{

ArrayList list = new ArrayList();

int i = 10;

string s = "Test";

list.Add(i);

list.Add(s);

// run it without/with commenting the following 2 lines

s = "hello";

i = 90;

Console.WriteLine(string.Format("{0}, {1}\n", i, s));

Console.WriteLine(list.Contains(i));

Console.ReadKey();

}

It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?

muthu

http://techblog.muthuka.com/


Dec 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Qun Cao | last post by:
Hello, I am just starting to play threading in python, here is a really interesting problem I am very curious about: " import thread def main(): thread.start_new(test.()) def test():
0
by: Niranjan | last post by:
Access XP Windows XP This code has been working for over 5 years with no problems and all of a sudden I am running into these wierd problems. I have this code to delete a record....
1
by: paul reed | last post by:
Hello, I am having some weird behavior between two machines...one which is running the 1.1 framework and one which is running 1.0. After opening a child form from a parent...I update the...
3
by: Michael Loughry | last post by:
I'm working for a company in Houston developing a web application. At one point in the code, we have to refresh the page, but save what checkboxes have been selected. Since these checkboxes are...
112
by: Tom | last post by:
This is very strange: I have a Windows Form with a Panel on it. In that panel I dynamically (at run time) create some labels, as so: for i=1 to x dim ctlNew as New Label() with ctlNew...
14
by: SStory | last post by:
I am trying to make a splash screen for my vb.net app. It is an mdi app. including the splash code produces wierd results. not inluding makes things fine. Also have tried loading the splash...
0
by: Tom | last post by:
OK, here's a wierd one... I have a listbox, which I fill with strings (in my case, file names). I normally load this via a loop, adding each item to the list box in the loop. I put lb.BeginUpdate...
3
by: Tom | last post by:
We are experiencing some wierd debugging behavior. What happens is that, during debugging with VS 2003, the debugger seems to 'skip' statements that are associated with database operations. For...
0
by: Tom | last post by:
I have some very strange issues with combo boxes on a tab control. Here's the scenario: I have a Windows Forms form that has a tab control on it, with two (2) tabs. Tab 2 happens to have a number...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.