473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1376
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
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...
3
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
24
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...
16
by: Ed A | last post by:
Hi all: I'm using a struct of type Point that is being passed on to a method as a refrence type (boxing), the UpdatePoint method changes the coordinates of this point as such: Point p = new...
2
by: Mike D Sutton | last post by:
Please excuse the terribly 'newbie'ness of this question, unfortunately my C# is very rusty.. What I'm trying to do is write a simple interactive drawing application where a few lines can be moved...
5
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
13
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...
94
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. ...
161
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.