473,626 Members | 3,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ToStr ing();

stat[1] = txt2.Text.ToStr ing();

stat[2] = txt3.Text.ToStr ing();

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 1912
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*******@hotm ail.com> wrote in message news:ug******** ******@TK2MSFTN GP15.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.ToStr ing();

stat[1] = txt2.Text.ToStr ing();

stat[2] = txt3.Text.ToStr ing();

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.ToStr ing();

stat[1] = txt2.Text.ToStr ing();

stat[2] = txt3.Text.ToStr ing();

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.AddRa nge(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*******@hotm ail.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.ToStr ing();

stat[1] = txt2.Text.ToStr ing();

stat[2] = txt3.Text.ToStr ing();

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
5966
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 been reading this ng all semester, and found it very helpful. I just have a stupid problem going on right now. Here's the project: We have to make a little frame with a text field, where the user inputs a number, and a text area where a...
5
4750
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 i'm going to have. Simon.
2
6850
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 replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
1
7263
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, ArrayList. Thanks, Mohan
4
2812
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 that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
18
3237
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 storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
14
3123
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 very unhappy with the idea and the compiler refuses to play ball with any of the attempts I have made to build such a structure. There is no problem when using a single string but a two dimensional array of strings appears to be a very different...
5
6513
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
5874
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 number that shows up the most. And the third one is to find a standard deviation, which I will get to later. Anyway, right now I'm doing Average. public class Statistics { public static void main(String args) { ArrayList <Integer>...
0
8272
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8514
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.