471,317 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Simple Unboxing Question

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?

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

Dec 3 '05 #1
6 1304
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.
}

}

Dec 3 '05 #2
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?

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

"Zeya" wrote:
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.
}

}

Dec 3 '05 #3
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
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
Dec 3 '05 #4
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

Dec 3 '05 #5
wud this a better idea?

using System.Collections.Specialized;

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

regards
Dec 3 '05 #6
Bill,

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

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

"Bill Butler" wrote:
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
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

Dec 3 '05 #7

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
16 posts views Thread by Ed A | last post: by
2 posts views Thread by Mike D Sutton | last post: by
5 posts views Thread by chenedor | last post: by
13 posts views Thread by gökhan | last post: by
94 posts views Thread by Peter Olcott | 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.