entering an array of numbers into an arraylist

  • Thread starter Harold A. Mackey
  • Start date
H

Harold A. Mackey

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
 
K

Kai Brinkmann [MSFT]

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; // 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.
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
 
T

tarasn

here is the better way to do the same thing:

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

Taras
 
M

Morten Wennevik

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.



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
 

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