Saturday, June 13, 2009

Appending ArrayList to another ArrayList

A friend of mine asked about how to append an ArrayList to another ArrayList in C#. I found out that it's an easy task as long as the type of the ArrayList is the same. Just have to typecast it to ICollection.
e.g.

ArrayList a = new ArrayList();
a.Add("Array 1");
a.Add("Array 2");

ArrayList b = new ArrayList();
b.Add("Array 3");
b.Add("Array 4");

a.AddRange((ICollection)b);
Share:

4 comments:

  1. we do have this in our programming also
    this is one of the basic ways to learn hardcore programming.

    ReplyDelete
  2. Hahaha, u do? I didn't, I only had the simple one, typecasting in primitive data type, in classes. hehehe...

    ReplyDelete
  3. thx your code.

    I have more guess...
    How to access and change arrayList's value of arrayList?

    e.g.
    ArrayList a = new ArrayList();
    ArrayList b = new ArrayList();
    b.Add("apple");
    b.Add("banana");
    b.Add("melon");

    a.Add(b); //right?

    I wanna chang "apple" -> "kiwi"

    How I do?

    ReplyDelete
  4. I don't think it's possible to change the value of an object in an ArrayList without re-adding it. You have to loop through the items and change it manually.

    ReplyDelete

You may be intersted in

Related Posts

Updating Table Containing Xml Column via LinkedServer

If you are trying to update a table containing XML column via Linked Server in SQL Server, and you are not able to, you are not alone. There...

About Me

My photo
Is an ordinary man, with a little knowledge to share and high dreams to achieve. I'd be glad if I can help others, 'coz the only thing for the triumph of evil is for a good man to do nothing.

About Blog

You can find a lot of debugging and deploying problems while developing applications in .NET and Visual Basic here. There are also some querying tips in SQL and typical source codes which might be useful shared here.

Popular Posts

Blogroll

Followers

Leave a Message