Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ArrayList with restricted access

ArrayList with restricted access

Scheduled Pinned Locked Moved C#
helpcsharpperformance
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark T
    wrote on last edited by
    #1

    I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007

    S L S 3 Replies Last reply
    0
    • M Mark T

      I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Instead of returning the ArrayList itself you could return an enumerator for the ArrayList. Take a look at the ArrayList.GetEnumerator method.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      • M Mark T

        I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        If the only action you want to offer is enumeration, return an enumerator. If you want to offer a larger subset of ArrayList functionality, create your own class that: - either inherits from ArrayList and hides the properties/methods you dont want to offer, - or contains an ArrayList and makes public the enumerator plus those extra properties/ methods you want to export. :)

        Luc Pattyn

        M 1 Reply Last reply
        0
        • L Luc Pattyn

          If the only action you want to offer is enumeration, return an enumerator. If you want to offer a larger subset of ArrayList functionality, create your own class that: - either inherits from ArrayList and hides the properties/methods you dont want to offer, - or contains an ArrayList and makes public the enumerator plus those extra properties/ methods you want to export. :)

          Luc Pattyn

          M Offline
          M Offline
          Mark T
          wrote on last edited by
          #4

          Thanks to you both who responded. I couldn't figure it out without you. For others interested, here is a minimal solution class which seems to work so far. You may want to add other properties or functions, but this is what I need for now. I call it a Firm List since, for the user only, the array size is no longer variable but is firm (solidified). The user can read and use each item in the list, but can't make the list contain a different item nor change the number of items in the list.

          using System;
          using System.Collections;

          namespace FreeForAll {
          public class FirmList : IEnumerable {
          ArrayList foundation;

          public FirmList(ArrayList source) {
            foundation = source;
          }
          
          public IEnumerator GetEnumerator() {
            return foundation.GetEnumerator();
          }
          
          public bool Contains(object item) {
            return foundation.Contains(item);
          } 
          
          public int Count {
            get { return foundation.Count; }
          }
          

          }
          }

          Here is how, within another class, I return a FirmList as property "SomeList". Within this class, I have full accees to the ArrayList itself and all of its abilities.

          class A {
          ArrayList TheRealList = new ArrayList();
          FirmList TheFirmList;
          A() { // constructor
          TheFirmList = new FirmList(TheRealList);
          }
          ...
          void SomeFunctionThatAddsMembersToTheArrayList() {...};
          ...
          public FirmList SomeList {
          get { return TheFirmList; }
          }
          }

          And, finally, here is how some other code might use it, just like and ArrayList is used:

          A aaa = new A();
          aaa.SomeFunctionThatAddsMembersToTheArrayList();
          FirmList list = A.SomeList;
          foreach (object o in list) {
          ...
          }
          WriteLine("{0}", list.Count);
          if (list.Contains(...)) {...

          Hope this helps someone else. Mark

          1 Reply Last reply
          0
          • M Mark T

            I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007

            S Offline
            S Offline
            Syed Muhammad Kamran
            wrote on last edited by
            #5

            In C# you can't mark reference types as readonly. However you can Encapsulate ArrayList inside a class and provide restricted access to it through methods, properties. You mentioned that speed is an issue otherwise you could have simply returned a clone to it.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups