473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 5405
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: <html><body> <div id='left' style='float:left;...
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 a whole for loop? Best Regards Terry
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 mycount1; unsigned short mycount2; unsigned short...
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 all the information using: for index =...
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 the textboxes. This is really easy in VB6 - im sure...
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 someone put it, but can someone explain what the...
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 default. Any suggestions? Thanks, Adam
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 Kis-Adam <dfighter_AT-NOSPAM_freemail.hu
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--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.