Simple Unboxing Question

G

Guest

I have a situation where I have pairs of strings that I want to put into an
ArrayList and then later pull out of it.

To keep things simple I tried this to put them in:

arrayList.Add(new object[] {stringA, stringB});


That works fine. But I can't figure out how to unbox them into their
constituent components. Might anyone know a way? Or am I forced to create
simple class to define the object that the strings will initially be placed
into?
 
Z

Zeya

for ( int i = 0; i < arrayList.length; i++ )
{
object TempObject[] = arrayList [ i ];
for ( int j=0; j< TempObject.length; j++ )
{
console.writeline ( "i " + i + " j: " + (string)TempObject[ j ]
);
//or do whatever you want to do with each entry.
}

}
 
G

Guest

But Zeya, this only pulls one string out of the object. There are actually
two strings in every object element in the ArrayList.

Or am I missing something?
 
B

Bill Butler

Robert W. said:
I have a situation where I have pairs of strings that I want to put into an
ArrayList and then later pull out of it.

To keep things simple I tried this to put them in:

arrayList.Add(new object[] {stringA, stringB});


That works fine. But I can't figure out how to unbox them into their
constituent components. Might anyone know a way? Or am I forced to create
simple class to define the object that the strings will initially be placed
into?

Hi,

You don't need to use an Array of object. You can use a string array instead.

Anyway... here is a sample program

using System;
using System.Collections;

class Test
{
public static void Main()
{
string[] pair1 = {"happy","Birthday"};
string[] pair2 = {"Peanut butter","Jelly"};
string[] pair3 = {"Good","Evil"};

// Fill ArrayList
ArrayList list = new ArrayList();
list.Add(pair1);
list.Add(pair2);
list.Add(pair3);


foreach(string[] pair in list)
{
Console.WriteLine("{0} AND {1}",pair[0], pair[1]);
}
}
}

OUTPUT
---------------------
happy AND Birthday
Peanut butter AND Jelly
Good AND Evil

Hope this helps
Bill
 
Z

Zeya

There could be two reasons:
1. If you did not correct my typo ( I am not sure if this even compiles
this way)
2. The loop index.

Try following:

for ( int i = 0; i < arrayList.length; i++ )
{
object[] TempObject = arrayList [ i ]; &larr;
for ( int j=0; j< TempObject.length; j++ ) &larr; change j <=
TempObject.length
{
console.writeline ( "i " + i + " j: " + (string)TempObject[ j ]
);
//or do whatever you want to do with each entry.
}

HTH
 
G

Guest

wud this a better idea?

using System.Collections.Specialized;

//...
NameValueCollection col = new NameValueCollection();
col.Add("key1", "value1");
string val = col["key1"];
//...

regards
 
G

Guest

Bill,

Yes, yes, simple but brilliant! I wish I had thought of that!

Thanks!!


--
Robert W.
Vancouver, BC
www.mwtech.com



Bill Butler said:
Robert W. said:
I have a situation where I have pairs of strings that I want to put into an
ArrayList and then later pull out of it.

To keep things simple I tried this to put them in:

arrayList.Add(new object[] {stringA, stringB});


That works fine. But I can't figure out how to unbox them into their
constituent components. Might anyone know a way? Or am I forced to create
simple class to define the object that the strings will initially be placed
into?

Hi,

You don't need to use an Array of object. You can use a string array instead.

Anyway... here is a sample program

using System;
using System.Collections;

class Test
{
public static void Main()
{
string[] pair1 = {"happy","Birthday"};
string[] pair2 = {"Peanut butter","Jelly"};
string[] pair3 = {"Good","Evil"};

// Fill ArrayList
ArrayList list = new ArrayList();
list.Add(pair1);
list.Add(pair2);
list.Add(pair3);


foreach(string[] pair in list)
{
Console.WriteLine("{0} AND {1}",pair[0], pair[1]);
}
}
}

OUTPUT
---------------------
happy AND Birthday
Peanut butter AND Jelly
Good AND Evil

Hope this helps
Bill
 

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