473,388 Members | 1,209 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,388 software developers and data experts.

entering an array of numbers into an arraylist

I have a situation where a user must enter three int results into three textboxes, save that entry and enter another set. All sets have to be displayed in another box or grid, and when the collection of data is complete, the array of arrays must be saved.

I created an array and populated the elements:
string[] stat = new string[3];

stat[0] = txt1.Text.ToString();

stat[1] = txt2.Text.ToString();

stat[2] = txt3.Text.ToString();

And added this array to the arraylist:

stats.Add(new stat());

stats.Count tells me that I am adding another array to the list whenever I btn_Click

lblResults.Text ="The count is: " + stats.Count + "\n";

But I cannot iterate through the arraylist to see the results.

for( i=0; i!=stats.Count; i++)

{

string result = stats[0].ToString();

lblResults.Text += result + "\n";

}

The question, I think, is how do you unbox and array from an arraylist? Or is there a better way to do this? :>)

Many Thanks,

Harry

Nov 17 '05 #1
3 1904
Keep in mind that your ArrayList stats contains generic object references. You need to cast these objects to their correct type when retrieving them from your array list. Your code below:

string result = stats[0].ToString();

retrieves an object reference from the array list and then attempts to cast this as a string. Try something like this instead:

foreach (string[] results in stats)
{
foreach (string result in results)
{
lblResults.Text += result + "\n";
}
}

The difference here is that you're casting each object retrieved from stats as a string array. Using the foreach loop is the equivalent of using a for loop - as you did below - and then retrieving each object like this:

string[] results = (string[]) stats[i]; // observe the cast to type string[]

Also remember that you are actually retrieving references to string arrays from the array list, so you still need to iterate through each individual string array to retrieve the individual strings.

I hope this helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Harold A. Mackey" <ma*******@hotmail.com> wrote in message news:ug**************@TK2MSFTNGP15.phx.gbl...
I have a situation where a user must enter three int results into three textboxes, save that entry and enter another set. All sets have to be displayed in another box or grid, and when the collection of data is complete, the array of arrays must be saved.

I created an array and populated the elements:
string[] stat = new string[3];

stat[0] = txt1.Text.ToString();

stat[1] = txt2.Text.ToString();

stat[2] = txt3.Text.ToString();

And added this array to the arraylist:

stats.Add(new stat());

stats.Count tells me that I am adding another array to the list whenever I btn_Click

lblResults.Text ="The count is: " + stats.Count + "\n";

But I cannot iterate through the arraylist to see the results.

for( i=0; i!=stats.Count; i++)

{

string result = stats[0].ToString();

lblResults.Text += result + "\n";

}

The question, I think, is how do you unbox and array from an arraylist? Or is there a better way to do this? :>)

Many Thanks,

Harry

Nov 17 '05 #2
here is the better way to do the same thing:

foreach(string result in stats )
{
lblResults.Text += result + "\n";
}

Taras

I have a situation where a user must enter three int results into
three textboxes, save that entry and enter another set. All sets have
to be displayed in another box or grid, and when the collection of
data is complete, the array of arrays must be saved.

I created an array and populated the elements:
string[] stat = new string[3];
stat[0] = txt1.Text.ToString();

stat[1] = txt2.Text.ToString();

stat[2] = txt3.Text.ToString();

And added this array to the arraylist:

stats.Add(new stat());

stats.Count tells me that I am adding another array to the list
whenever I btn Click

lblResults.Text ="The count is: " + stats.Count + "\n";

But I cannot iterate through the arraylist to see the results.

for( i=0; i!=stats.Count; i++)

{

string result = stats[0].ToString();

lblResults.Text += result + "\n";

}

The question, I think, is how do you unbox and array from an
arraylist? Or is there a better way to do this? :>)

Many Thanks,

Harry


Nov 17 '05 #3
Harold,

It may be that you are looking for ArrayList.AddRange(ICollection)

stats.AddRange(stat);
for(i=0; i!=stats.Count; i++)
{
string result = stats[0].ToString();
lblResults.Text += result + "\n";
}

Btw, please stick to text only, no html formatting.
As tarasn said, foreach would be simpler to use in this case.

On Thu, 05 May 2005 16:55:39 +0200, Harold A. Mackey <ma*******@hotmail.com> wrote:
I have a situation where a user must enter three int results into three textboxes, save that entry and enter another set. All sets have to be displayed in another box or grid, and when the collection of data is complete, the array of arrays must be saved.

I created an array and populated the elements:
string[] stat = new string[3];

stat[0] = txt1.Text.ToString();

stat[1] = txt2.Text.ToString();

stat[2] = txt3.Text.ToString();

And added this array to the arraylist:

stats.Add(new stat());

stats.Count tells me that I am adding another array to the list whenever I btn_Click

lblResults.Text ="The count is: " + stats.Count + "\n";

But I cannot iterate through the arraylist to see the results.

for( i=0; i!=stats.Count; i++)

{

string result = stats[0].ToString();

lblResults.Text += result + "\n";

}

The question, I think, is how do you unbox and array from an arraylist? Or is there a better way to do this? :>)

Many Thanks,

Harry



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
5
by: Simon Johnson | last post by:
In c# is it possible to resize an array cleanly? My application is that I have a list of numbers i'd like to return from a method as an integer array. I don't know before hand how many integers...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
1
by: Mohan | last post by:
Hi, In my application, I need to increase the 2-dimensional array size dynamically. Please let me know the procedure to declare a dynamic 2-dimensional array like Single dimensional array,...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
5
by: John | last post by:
How can I fill an array randomly so it contains a certian range of numbers (1 - 100 for example) ? My Goal is to generate a set of numbers in random order.
22
by: Energizer100 | last post by:
Hey. I'm having some difficulty in Array lists. I have to find the average of all the numbers in an array list. That's my first task. My second one is to find the mode of all the numbers, meaning the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.