473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Allowing access to an internal ArrayList through IEnumerator?

Hi,

I'm trying to provide the user of my class access to the objects in a
collection without allowing him to modify (add/remove/clear items) the
collection. I thought I can do it by providing a IEnumerator. But this
thingy can't be used directly, as shown in the code below. How can this
be done?

Thanks in advance.

Matthias

Here the test I tried:

using System;
using System.Collecti ons;
using System.Collecti ons;

public class MyClass
{
public static void Main()
{
string s1 = "Hello";
string s2 = "World!";
a.Add(s1);
a.Add(s2);

foreach (String s in GetEnumerator() )
Console.WriteLi ne(s);
}

private static IEnumerator GetEnumerator() {
return a.GetEnumerator ();
}
private static ArrayList a = new ArrayList();
}
Nov 16 '05 #1
1 2482
Matthias,

An enumerator is used for enumerating through the items in a list, not
for providing read-only access. If you want to provide read-only access to
an ArrayList, then call the static ReadOnly method on the ArrayList class,
which will return an ArrayList which is read-only (it will throw an
exception if you try and modify the list in any way).

Be careful though, if you have object references in your array list,
then your objects could be modified, they are not read only, just the list.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Matthias S." <po*****@emvoid SPAMTRAP.de> wrote in message
news:ud******** ******@TK2MSFTN GP14.phx.gbl...
Hi,

I'm trying to provide the user of my class access to the objects in a
collection without allowing him to modify (add/remove/clear items) the
collection. I thought I can do it by providing a IEnumerator. But this
thingy can't be used directly, as shown in the code below. How can this be
done?

Thanks in advance.

Matthias

Here the test I tried:

using System;
using System.Collecti ons;
using System.Collecti ons;

public class MyClass
{
public static void Main()
{
string s1 = "Hello";
string s2 = "World!";
a.Add(s1);
a.Add(s2);

foreach (String s in GetEnumerator() )
Console.WriteLi ne(s);
}

private static IEnumerator GetEnumerator() {
return a.GetEnumerator ();
} private static ArrayList a = new ArrayList();
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1886
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to pItem. As I understand it (and I coded it to test) if I try to use pAttribute again by clearing with pAttribute.Clear because it is a shallow copy...
1
4910
by: Kevin P. Fleming | last post by:
ArrayList implements an overload for IEnumerator's GetEnumerator method as follows: GetEnumerator(int offset, int count) However, I can't see a way to take advantage of this, even though I'd like to... I'd like to be able to use foreach to iterate over a section of the ArrayList, but can't find any syntax that will work. The obvious: ...
2
3850
by: steve smith | last post by:
Hi I am new to this language, and still don't understand most of the concepts, I am trying to sort an arraylist by alphabet, I belive i need to use the IComparer interface, but have no idea how this works? Does anyone have a sample of how i can sort my arraylist of words. Also how is an arraylist iterated over and is it possible to obtain a...
6
2028
by: Fernando Rodríguez | last post by:
Hi, I have a class called UrlFetcher and keep several instances inside an ArrayList. This class has a fecth() method. When I iterate through the list calling the fetch() method, I have to downcast from object to UrlFetch. This is pretty ugly... -------------------------------------------------------------- IEnumerator fetcher =...
2
5064
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the ArrayList seemed to make the most sense. When I use it I get the error: At least one object must implement IComparable. Here's the code that produces that...
18
6626
by: Rob Panosh | last post by:
Hello, When traversing an ArrayList which is faster? For Each oItem as Something in me.ArrayList ..... ..... Next OR
20
5958
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim...
1
1736
by: Joe | last post by:
Hello All: I am writing a function which accepts an ArrayList as its parameter and converts the contents of the ArrayList into an XmlNodeList. The ArrayList will contain one of several different structures. For example, the structures could be either a Customer structure (which contains a customer id and name) or an Address structure...
3
4318
by: starter | last post by:
I am trying to learn IEnumerator and collections(ArrayList, Hashtable) in .NET. Can anyone tell me what is IEnumerator used for and any online resources would be helpful. Thanks
0
7888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7811
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.