Replace value in Arraylist

S

Steven

I have an arraylist and I want to loop through the array and replace all the
""(or blank) values with string name "Pico". I'm doing this ..
foreach (String item in this.List1)
{
if (item == "")
{ item.Replace("","Pico"); // line 1 }
}

but when I'm trying to run the program -- its giving me compilation error "
parameter old value cannot be of zero length in line 1". How can I achieve
this?



Steven
 
B

Brendan Grant

If you aren't completely intent on using a foreach, you
could simply switch to:

for(int x = 0; x < this.List1.Count; x++)
{
if((string)this.List1[x] == "")
{
this.List1[x] = "Pico";
}
}
 
S

Steven

Thanks Brendan! That works

-- Steven

Brendan Grant said:
If you aren't completely intent on using a foreach, you
could simply switch to:

for(int x = 0; x < this.List1.Count; x++)
{
if((string)this.List1[x] == "")
{
this.List1[x] = "Pico";
}
}
-----Original Message-----
I have an arraylist and I want to loop through the array and replace all the
""(or blank) values with string name "Pico". I'm doing this ..
foreach (String item in this.List1)
{
if (item == "")
{ item.Replace("","Pico"); // line 1 }
}

but when I'm trying to run the program -- its giving me compilation error "
parameter old value cannot be of zero length in line 1". How can I achieve
this?



Steven


.
 
P

Paul

Steven said:
I have an arraylist and I want to loop through the array and replace all the
""(or blank) values with string name "Pico". I'm doing this ..
foreach (String item in this.List1)
{
if (item == "")
{ item.Replace("","Pico"); // line 1 }
}

but when I'm trying to run the program -- its giving me compilation error "
parameter old value cannot be of zero length in line 1". How can I achieve
this?



Steven
Just an FYI, your code compiled for me with no errors. I cut and pasted
your code and used

ArrayList List1 = new ArrayList();

Thats all.

Paul
 

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