Clearing arrays

  • Thread starter Thread starter Liz - Newbie
  • Start date Start date
L

Liz - Newbie

Does anyone know how to clear arrays? My C# books talk
about creating arrays or talk about using Clear or
RemoveAt but these methods don't appear to be available
for my array.

I have an array of client objects called aClients

if I try aClients.RemoveAt I get an error
"System.Array" does not contain a definition
for "RemoveAt".

if I try aClients.Clear() I get an error
No overload for method "Clear" takes "0" Arguments
 
Hi,

There's a Dr. GUI's article on using arrays in .NET called

"Dr. GUI .NET #7
Conway's Game of Life as a Windows Forms Application"

You can find this article on the MSDN Library.
 
The book is talking about ArrayList

ArrayList aClients = new ArrayList();

aClients.Add(client1);
aClients.Add(client2);
aClients.Add(client3);

aClients.Remove(client3);
aClients.RemoveAt(0);
aClients.Clear();
 
Liz - Newbie said:
Does anyone know how to clear arrays? My C# books talk
about creating arrays or talk about using Clear or
RemoveAt but these methods don't appear to be available
for my array.

Are you sure they're talking about arrays rather than ArrayLists? It's
rare to use those operations on arrays.
I have an array of client objects called aClients

if I try aClients.RemoveAt I get an error
"System.Array" does not contain a definition
for "RemoveAt".
if I try aClients.Clear() I get an error
No overload for method "Clear" takes "0" Arguments

Removing an item from an array (or clearing the array) doesn't really
make sense, as arrays have fixed lengths. However, you *can* do it by
casting the reference to type IList first - Array implements various
IList members using explicit interface implementation.
 
Hi Liz,

Liz - Newbie said:
Does anyone know how to clear arrays? My C# books talk
about creating arrays or talk about using Clear or
RemoveAt but these methods don't appear to be available
for my array.

Actually it isn't. Use an ArrayList class for that.
I have an array of client objects called aClients

if I try aClients.RemoveAt I get an error
"System.Array" does not contain a definition
for "RemoveAt".
Yup.

if I try aClients.Clear() I get an error
No overload for method "Clear" takes "0" Arguments

Try using Array.Clear(aClients) static method.
 
Hi Jon,

Jon Skeet said:
Removing an item from an array (or clearing the array) doesn't really
make sense, as arrays have fixed lengths. However, you *can* do it by
casting the reference to type IList first - Array implements various
IList members using explicit interface implementation.

I am not sure you can - this is what docs say:
.... Implements IList.RemoveAt. Always throws NotSupportedException
And it actually throws it.
Am I missing something?
 
I am not sure you can - this is what docs say:
... Implements IList.RemoveAt. Always throws NotSupportedException
And it actually throws it.
Am I missing something?

No, by "can do it" I meant "can call it". (I'd originally had that
paragraph elsewhere, specifically talking about Clear, where it made
more sense.)

So yes, you can get to the stage where it will compile, but then it
won't do anything useful.
 
If you don't know the size of the Array, or if you want a dynamic array
with added functionality use an ArrayList instead.
When retrieving from an ArrayList, remember to cast the object to whatever
type you passed it.

ArrayList myList = new ArrayList();
string line1 = "1";
string line2 = "2";
string line3 = "3";

myList.Add(line1);
myList.Add(line2);
myList.Add(line3);

string line4 = (string)myList[0];

foreach(string str in myList)
{
WriteLine(str); // no casting required since it's done in the foreach
statement
}
 
Add

using System.Collections
or
(VB.NET)
imports System.Collections

--
Miha Markic - DXSquad/RightHand .NET consulting & software development
miha at rthand com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.
 
I think maybe I should be using hashtable to deal with
arrays of objects that can be added or removed.
 
Back
Top