471,309 Members | 1,066 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

UnBoxing

wg
I am attempting to update a value in an arraylist. However when I updated
one element, all elements get updated to the same value. Does anyone know
how to achieve this?

Here is an example,

private ArrayList TagList = new ArrayList();

private void button1_Click(object sender, System.EventArgs e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
Console.Write("---------------------");

}

private void button2_Click(object sender, System.EventArgs e)
{

((TagHolder)TagList[0]).Tag1 = (int) 5;
((TagHolder)TagList[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
}
public class TagHolder
{
private int tag1;
private string tag2;

public int Tag1
{
get
{
return tag1;
}
set
{
tag1 = value;
}
}
public string Tag2
{
get
{
return tag2;
}
set
{
tag2 = value;
}
}

}

Thanks

wg
Jan 27 '06 #1
4 1082
If you're looking to create multiple objects for the ArrayList then you'll
need to create multiple instances.

TagHolder firstTag = new TagHolder();
firstTag.Tag1 = 1;
firstTag.Tag2 = "Wade";
TagList.Add(firstTag);

TagHolder secondTag = new TagHolder();
secondTag.Tag1 = 2;
secondTag.Tag2 = "Joe";
TagList.Add(secondTag);

--
Tim Wilson
..NET Compact Framework MVP

"wg" <wg@somewhere.com> wrote in message
news:hB****************@bignews7.bellsouth.net...
I am attempting to update a value in an arraylist. However when I updated
one element, all elements get updated to the same value. Does anyone know
how to achieve this?

Here is an example,

private ArrayList TagList = new ArrayList();

private void button1_Click(object sender, System.EventArgs e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
Console.Write("---------------------");

}

private void button2_Click(object sender, System.EventArgs e)
{

((TagHolder)TagList[0]).Tag1 = (int) 5;
((TagHolder)TagList[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
}
public class TagHolder
{
private int tag1;
private string tag2;

public int Tag1
{
get
{
return tag1;
}
set
{
tag1 = value;
}
}
public string Tag2
{
get
{
return tag2;
}
set
{
tag2 = value;
}
}

}

Thanks

wg

Jan 27 '06 #2
TagHolder is a reference type, therefore you only put a reference to the
object in your ArrayList. All the elements of your ArrayList hold the same
reference so when you change one, you change them all.

Implement ICloneable in your TagHolder class, and in the Clone() method,
create a deep copy of the first TagHolder for each ArrayList element.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"wg" wrote:
I am attempting to update a value in an arraylist. However when I updated
one element, all elements get updated to the same value. Does anyone know
how to achieve this?

Here is an example,

private ArrayList TagList = new ArrayList();

private void button1_Click(object sender, System.EventArgs e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
Console.Write("---------------------");

}

private void button2_Click(object sender, System.EventArgs e)
{

((TagHolder)TagList[0]).Tag1 = (int) 5;
((TagHolder)TagList[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
}
public class TagHolder
{
private int tag1;
private string tag2;

public int Tag1
{
get
{
return tag1;
}
set
{
tag1 = value;
}
}
public string Tag2
{
get
{
return tag2;
}
set
{
tag2 = value;
}
}

}

Thanks

wg

Jan 27 '06 #3
wg
Thanks for the advice, makes sense. Now my delima, I am attempting to create
a class to load info from a spreadsheet of unkown length. Then I am
attempting to update values in the arraylist. Any suggestions.

Thanks

wg
"wg" <wg@somewhere.com> wrote in message
news:hB****************@bignews7.bellsouth.net...
I am attempting to update a value in an arraylist. However when I updated
one element, all elements get updated to the same value. Does anyone know
how to achieve this?

Here is an example,

private ArrayList TagList = new ArrayList();

private void button1_Click(object sender, System.EventArgs e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
Console.Write("---------------------");

}

private void button2_Click(object sender, System.EventArgs e)
{

((TagHolder)TagList[0]).Tag1 = (int) 5;
((TagHolder)TagList[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
}
public class TagHolder
{
private int tag1;
private string tag2;

public int Tag1
{
get
{
return tag1;
}
set
{
tag1 = value;
}
}
public string Tag2
{
get
{
return tag2;
}
set
{
tag2 = value;
}
}

}

Thanks

wg

Jan 27 '06 #4
Assuming you're working in framework version 1.1, I suggest creating a custom
collection inherited from CollectionBase. You can find an example in my
ScriptCollection in my article at
http://www.dalepreston.com/Blog/2005...s-to-your.html
or an even more detailed example at
http://weblogs.asp.net/ngur/articles/144770.aspx.

After you've created a few custom collections like that it will only take
you about 10 to 30 minutes to create a new collection type when you need one
depending on what additional customizations you need. That time spent up
front will save you a lot of time as you continue to develop your
application. You'll have a strongly typed collection that should eliminate
the confusion you're having with ArrayList.

Create a class representing your Excel data, iterate through your Excel
data, creating instances of your Excel data class, and add those items to
your Excel data collection class. Now you can easily manipulate your data
with intellisense and common framework functionality.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"wg" wrote:
Thanks for the advice, makes sense. Now my delima, I am attempting to create
a class to load info from a spreadsheet of unkown length. Then I am
attempting to update values in the arraylist. Any suggestions.

Thanks

wg
"wg" <wg@somewhere.com> wrote in message
news:hB****************@bignews7.bellsouth.net...
I am attempting to update a value in an arraylist. However when I updated
one element, all elements get updated to the same value. Does anyone know
how to achieve this?

Here is an example,

private ArrayList TagList = new ArrayList();

private void button1_Click(object sender, System.EventArgs e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag);
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
Console.Write("---------------------");

}

private void button2_Click(object sender, System.EventArgs e)
{

((TagHolder)TagList[0]).Tag1 = (int) 5;
((TagHolder)TagList[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLine( ((TagHolder)TagList[0]).Tag1);
Console.Write(((TagHolder)TagList[0]).Tag2.ToString());
Console.WriteLine( ((TagHolder)TagList[1]).Tag1);
Console.Write(((TagHolder)TagList[1]).Tag2.ToString());
}
public class TagHolder
{
private int tag1;
private string tag2;

public int Tag1
{
get
{
return tag1;
}
set
{
tag1 = value;
}
}
public string Tag2
{
get
{
return tag2;
}
set
{
tag2 = value;
}
}

}

Thanks

wg


Jan 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Mountain Bikn' Guy | last post: by
3 posts views Thread by Steve | last post: by
6 posts views Thread by David Young | last post: by
5 posts views Thread by chenedor | last post: by
9 posts views Thread by | last post: by
13 posts views Thread by gökhan | last post: by
94 posts views Thread by Peter Olcott | last post: by
19 posts views Thread by ahjiang | last post: by
161 posts views Thread by Peter Olcott | last post: by
reply views Thread by rosydwin | last post: by

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.