ArrayLists and AddRange

T

t f

Hi

I have some code which does basically the following:

void A()
{
ArrayList al1 = new ArrayList();
al1.AddRange(GetSomeNames());
//do something with al1...
}

ArrayList GetSomeNames()
{
ArrayList al2 = new ArrayList();
foreach (string s in some array)
{
if (s != string.empty)
al2.Add(s);
}
return al2;
}

Now my problem is -> if GetSomeNames() returns an empty ArrayList then
al1.AddRange throws an exception saying it cannot add null which is fine. To
get around this I simply use a try { } catch { } but is there a cleaner ways
of doing it (i.e. not having to use the try catch)?

I could create a new arraylist with the returned value and then check its
count before doing an addrange on it but this seems to be a crazy way...

any thoughts or suggestions?

thanks
tf
 
B

Brian Gideon

Hi

I have some code which does basically the following:

void A()
{
ArrayList al1 = new ArrayList();
al1.AddRange(GetSomeNames());
//do something with al1...

}

ArrayList GetSomeNames()
{
ArrayList al2 = new ArrayList();
foreach (string s in some array)
{
if (s != string.empty)
al2.Add(s);
}
return al2;

}

Now my problem is -> if GetSomeNames() returns an empty ArrayList then
al1.AddRange throws an exception saying it cannot add null which is fine. To
get around this I simply use a try { } catch { } but is there a cleaner ways
of doing it (i.e. not having to use the try catch)?

I could create a new arraylist with the returned value and then check its
count before doing an addrange on it but this seems to be a crazy way...

any thoughts or suggestions?

thanks
tf

tf,

Strange. ArrayList.AddRange works fine on empty collections for me.
Can you post short, but complete program demonstrating the problem.

Brian
 
S

sherifffruitfly

any thoughts or suggestions?

I don't know if this is any less crazy, but the following two-line
extension of your code would obviate the need for try-catch, no?

void A()
{
ArrayList al1 = new ArrayList();
al1.AddRange(GetSomeNames());
al1.RemoveAt(0);
//do something with al1...

}

ArrayList GetSomeNames()
{
ArrayList al2 = new ArrayList();
foreach (string s in some array)
{
al2.Add("dummy");
if (s != string.empty)
al2.Add(s);
}
return al2;
}

This couples GetSomeNames() with A(). To de-couple, you could overload/
replace GetSomeNames(), so that it takes an argument indicating
whether or not non-empty return is to be guaranteed.


Alternatively, if concurrency problems aren't a worry, then a no-new-
variable version of your "crazy way" would suffice:

if (GetSomeNames() != null) Al1.AddRange(GetSomeNames());


hth,

cdj
It avoids a variable, at the cost of running the method twice.
 
S

sherifffruitfly

Strange. ArrayList.AddRange works fine on empty collections for me.
Can you post short, but complete program demonstrating the problem.

Is it just me, or is msdn contradictory (with itself) on the
following:

http://msdn2.microsoft.com/en-us/library/system.collections.arraylist.addrange.aspx

Parameters

c

The ICollection whose elements should be added to the end of the
ArrayList. The collection itself cannot be a null reference (Nothing
in Visual Basic), but it can contain elements that are a null
reference (Nothing in Visual Basic).

..
..
..

Remarks

ArrayList accepts a null reference (Nothing in Visual Basic) as a
valid value and allows duplicate elements.


???
 
J

Jon Skeet [C# MVP]

t f said:
I have some code which does basically the following:

void A()
{
ArrayList al1 = new ArrayList();
al1.AddRange(GetSomeNames());
//do something with al1...
}

ArrayList GetSomeNames()
{
ArrayList al2 = new ArrayList();
foreach (string s in some array)
{
if (s != string.empty)
al2.Add(s);
}
return al2;
}

Now my problem is -> if GetSomeNames() returns an empty ArrayList then
al1.AddRange throws an exception saying it cannot add null which is fine.

AddRange only throws that exception if you pass it null, which is very
different from passing it an empty ArrayList.

Here's code to demonstrate that:

using System;
using System.Collections;

class Test
{
static void Main()
{
Console.WriteLine ("Trying with null...");
TryAddRange (null);
Console.WriteLine ("Trying with empty list...");
TryAddRange (new ArrayList());
}

static void TryAddRange(ArrayList list)
{
try
{
new ArrayList().AddRange(list);
Console.WriteLine ("Success!");
}
catch (Exception e)
{
Console.WriteLine ("Failure: "+e.Message);
}
}
}
 
B

Brian Gideon

Is it just me, or is msdn contradictory (with itself) on the
following:

http://msdn2.microsoft.com/en-us/library/system.collections.arraylist...

Parameters

c

The ICollection whose elements should be added to the end of the
ArrayList. The collection itself cannot be a null reference (Nothing
in Visual Basic), but it can contain elements that are a null
reference (Nothing in Visual Basic).

.
.
.

Remarks

ArrayList accepts a null reference (Nothing in Visual Basic) as a
valid value and allows duplicate elements.

???

Hi,

When I first read that I was thinking the same thing. However, the
remarks section is not referring to the AddRange method specifically,
but speaking about the ArrayList in general. And it is true that an
ArrayList will allow null values as elements in the collection.

Brian
 
T

t f

Hi

I just looked at my code and spotted the problem... lol

i was checking "some array" and if it had a count of 0 i was return null ...

:)

thanks
tf
 
B

Brian Gideon

Hi

I just looked at my code and spotted the problem... lol

i was checking "some array" and if it had a count of 0 i was return null ...

:)

thanks
tf

Ahh...yes. That would do it :)
 
S

sherifffruitfly

On Mar 14, 2:59 pm, "sherifffruitfly" <[email protected]>
wrote:
When I first read that I was thinking the same thing. However, the
remarks section is not referring to the AddRange method specifically,
but speaking about the ArrayList in general. And it is true that an
ArrayList will allow null values as elements in the collection.

Brian

Gotcha - thanks!
 

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