472,328 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Clearing arrays


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
10 5320
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
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
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
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
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
<"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
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
System.Collections.ArrayList
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #9
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Niels | last post by:
Hi group, I have some problems with clearing floated divs. My usual method works fine in Konqueror, but not in Firefox. Here's an example: ...
28
by: Terry Andersen | last post by:
I have an array that I initialize to zero, like: Buffer = {0x00}; How do I in my code reset this array to all zeros ones more? Without running...
12
by: Hans B | last post by:
Please bear with me if I ask silly questions....I am a somewhat newbie to the C language.... If I had: struct test1_{ unsigned char...
1
by: .:: MaStErDoN ::. | last post by:
Hi! I'm working with arrays, saving on them information about files. All works fine but the problem is that when i finish using them and i remove...
3
by: Christopher | last post by:
Hi I need to know how to work with a control array in c#. I would like to clear the contents of a textbox array after adding up the values in...
65
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as...
13
by: Adam Honek | last post by:
Instead of the ZeroMemory API what would one use in VB.NET to clean an array using a custom structure? The .clear isn't a member of it by...
65
by: Leslie Kis-Adam | last post by:
Hi everyone! Does anyone know, if it is possible to clear the screen in ANSI C? If it is,then how? Any help would be appreciated. Laszlo...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--)...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.