By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

Clearing arrays

P: n/a

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
Nov 15 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Liz - Newbie" <ec******@cornwall.gov.uk> wrote in message
news:06****************************@phx.gbl...

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


Nov 15 '05 #2

P: n/a
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();

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3

P: n/a
Liz - Newbie <ec******@cornwall.gov.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

P: n/a
Hi Liz,

"Liz - Newbie" <ec******@cornwall.gov.uk> wrote in message
news:06****************************@phx.gbl...

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.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
Nov 15 '05 #5

P: n/a
Hi Jon,

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Liz - Newbie <ec******@cornwall.gov.uk> wrote: .... 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?

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
Nov 15 '05 #6

P: n/a
<"Miha Markic" <miha at rthand com>> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Liz - Newbie <ec******@cornwall.gov.uk> wrote:

...
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?


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

P: n/a
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
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #8

P: n/a
System.Collections.ArrayList
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #9

P: n/a
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 su*****@devexpress.com
Bug reports should be directed to: su*****@devexpress.com
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.

"Liz - Newbie" <ec******@cornwall.gov.uk> wrote in message
news:08****************************@phx.gbl...
ArrayList does not appear to be available

I get error - type ArrayList could not be found
-----Original Message-----

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
.

Nov 15 '05 #10

P: n/a
I think maybe I should be using hashtable to deal with
arrays of objects that can be added or removed.
-----Original Message-----

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
.

Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.