473,725 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bject sender, System.EventArg s e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[1]).Tag2.ToString ());
Console.Write("---------------------");

}

private void button2_Click(o bject sender, System.EventArg s e)
{

((TagHolder)Tag List[0]).Tag1 = (int) 5;
((TagHolder)Tag List[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[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 1151
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(fir stTag);

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

--
Tim Wilson
..NET Compact Framework MVP

"wg" <wg@somewhere.c om> wrote in message
news:hB******** ********@bignew s7.bellsouth.ne t...
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(o bject sender, System.EventArg s e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[1]).Tag2.ToString ());
Console.Write("---------------------");

}

private void button2_Click(o bject sender, System.EventArg s e)
{

((TagHolder)Tag List[0]).Tag1 = (int) 5;
((TagHolder)Tag List[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[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(o bject sender, System.EventArg s e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[1]).Tag2.ToString ());
Console.Write("---------------------");

}

private void button2_Click(o bject sender, System.EventArg s e)
{

((TagHolder)Tag List[0]).Tag1 = (int) 5;
((TagHolder)Tag List[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[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.c om> wrote in message
news:hB******** ********@bignew s7.bellsouth.ne t...
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(o bject sender, System.EventArg s e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[1]).Tag2.ToString ());
Console.Write("---------------------");

}

private void button2_Click(o bject sender, System.EventArg s e)
{

((TagHolder)Tag List[0]).Tag1 = (int) 5;
((TagHolder)Tag List[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[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
ScriptCollectio n 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.c om> wrote in message
news:hB******** ********@bignew s7.bellsouth.ne t...
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(o bject sender, System.EventArg s e)
{
TagHolder Tag = new TagHolder();
Tag.Tag1 = 1;
Tag.Tag2 = "Wade";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Tag.Tag1 = 2;
Tag.Tag2 = "Thomas";
TagList.Add(Tag );
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[1]).Tag2.ToString ());
Console.Write("---------------------");

}

private void button2_Click(o bject sender, System.EventArg s e)
{

((TagHolder)Tag List[0]).Tag1 = (int) 5;
((TagHolder)Tag List[0]).Tag2 = "Stacy";
//Expect to only change element 0, but in reality changes 0 and 1.
Console.WriteLi ne( ((TagHolder)Tag List[0]).Tag1);
Console.Write(( (TagHolder)TagL ist[0]).Tag2.ToString ());
Console.WriteLi ne( ((TagHolder)Tag List[1]).Tag1);
Console.Write(( (TagHolder)TagL ist[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
6881
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app performance. In the hopes of improving performance, we have tried to find a way to avoid the...
3
1796
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
24
2621
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
6
3733
by: David Young | last post by:
I'm using hashtables to transport data between layers in my app, but in so doing, it seems almost impossible to avoid unboxing. private static BusinessObject PackageBusinessObject(Hashtable myHashtable) { if(myHashtable!=null) { BusinessObject myBusinessObject = new BusinessObject(); myBusinessObject.Property1 = (string)myHashtable; myBusinessObject.Property2 = (int)myHashtable;
5
1776
by: chenedor | last post by:
Hi all I am a bit confuse about unboxing... what is the difference and what is best practice ? object o = __box(1234); int n = *dynamic_cast<__box int*>(o); of
9
1607
by: | last post by:
Execuse me. I do boxing, for example Employee class, as object: public object ManipulateObject(object objInput) { ... Object obj; obj = objInput; ...
13
1847
by: gökhan | last post by:
Hi! I fear this question might be too basic, however being a c++ veteran I have trouble to get a good desing in c# running. // a base class class Vector{}; // inherited class class VectorChild:Vector{}; class VectorChildChild:VectorChild{};
94
5678
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. Thanks.
19
13719
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
7848
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every other platform in every other language. Normally a value type is the actual data itself stored in...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.