UnBoxing

W

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
 
T

Tim Wilson

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);
 
G

Guest

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
 
W

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
 
G

Guest

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/07/visually-add-client-scripts-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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top