Looping around an arraylist in c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an array list alSearchCriteria which contains values which vary each
time its created. It will always have 15 items in the arraylist each time its
created. Some of the values in the array list will be a string called null. I
want to create some way of looping through the arraylist and pulling back all
the values which aren't null and displaying them on some control like a
asp:label in .net.

Does anyone know how I could write such a loop in c# and is this the best
way of doing it.

So basically to summarise I need to write a loop which goes around my
arraylist and prints all the values which are not null to a label contol.
Could someone please help me with this.
 
HI,

try this:

1)
string result = string.Empty;
foreach(string val in alSearchCriteria)
{
if(val != null && val != string.Empty)
result += val + "\r\n";
}

myLabel.Text = result;


2)
string result = string.Empty;
for( int i=0; i<alSearchCriteria.Count; i++)
{
string val = alSearchCriteria as string;
if(val != null && val != string.Empty)
result += val + "\r\n";
}

myLabel.Text = result;


But I don't understand why you put the null-values into the ArrayList,
but well...

Hope this helps,
 
Hi,

A simple foreach will solve your problem, as long as all the elements are
of the same type (seems to be your case) you will be ok

cheers,
 
Back
Top