How do I make an arraylist public

Go To


In File1 I created a class with 3 strings. I created another class with a public arraylist. I want this arraylist to be dynamic and the object it contains are the class with the 3 strings. I can access the members of the class in the file but not in a separate file.


public class SensorCollection
        public string ipAddress;
        public string portNumber;   
        public string physicalLocation;

        public DetectorCollection(string ipAddr, string portNum, string loc)
            this.ipAddress = ipAddr;
            this.portNumber = portNum;
            this.physicalLocation = loc;

    public class SensorCollectionArray
        public System.Collections.ArrayList SensorArrayList;

System.Collections.ArrayList DetectorArrayList = new System.Collections.ArrayList(); 
  DetectorArrayList.Add(new DetectorCollection(ipAddress, portNum, str));

So I can fill the array of classes but can't access it in a separate file. File 2

    AdvancedSettingsForm.SensorCollectionArray mainDetectorCollectionArray;
    System.Collections.ArrayList arrList;
2012-04-04 18:37
by user1313577
Your code cannot be compiled. Your constructor is DetectorCollection but your class is SensorCollection - Saeed Amiri 2012-04-04 18:46
Are the files in same namespace - Anurag Ranjhan 2012-04-04 19:05
Are you on .net 1.x or why are you using ArrayList instead of List<T> - CodesInChaos 2012-04-04 19:06


If you create a SensorCollectionArray like this:

SensorCollectionArray mySCA = new SensorCollectionArray();

Then you can access it's ArrayList like this (for example, to add an item):


Note however, that in the code you've posted, you didn't include a constructor for the SensorCollectionArray, so the SensorArrayList will be null after instantiation. So you can either set it to a separately instantiated ArrayList, or you can create the ArrayList within your SensorCollectionArray class.

Final note: You might want to look into the generic List(of T) class if you want to create a strongly typed collection

2012-04-04 19:01
by Matt Burland
I'd say he should definitely look into using generics. There's not really a compelling reason to use ArrayList if you're using .NET 2 or later - Daniel Mann 2012-04-04 19:15


Not entirely sure what're attempting to do, but I assume it's something like the below. Presumably, you're creating a collection of your sensors because you want to apply some rules of some kind before storing it to the collection.

"Is this a good sensor? It is? Add it to the collection!"

Otherwise, you could just use a

List<Sensor> mySensors;

and not really use a class that'll essentially doing the same things. Aside from that, like it's been mentioned there's not really a reason to use ArrayList. As Marc points out here, the most compelling reason to use ArrayList is if you're using .NET 1.1; otherwise, you should use the generic List collection and all the great things it does for you.

public class Sensor
    public string Ip{ get; set; }
    public string Port{ get; set; }
    public string PhysicalLocation{ get; set }

    public Sensor(string ipAddr, string portNum, string loc)
        Ip= ipAddr;
        Port= portNum;
        PhysicalLocation= loc;

public class SensorCollection
    private List<Sensor> sensors;

    public Sensor this[int i]
        get { return this.sensors[i]; }
        set { this.sensors[i] = value; }

    public IEnumerable<Sensor> Sensors
        get{ return this.sensors; }

    public SensorCollection()
        sensors = new List<Sensor>();

    public SensorCollection(string ip, string port, string location) : this()
        this.sensors.Add(new Sensor(ip, port, location));

    public SensorCollection(Sensor sensor) : this()

    public void AddSensor(Sensor sensor)
        //Determine whether or not to add it

    public void RemoveSensor(Sensor sensor)
        if (sensors.Contains(sensor))


How do I access the ipaddress of each sensor in my dynamically created list of classes?

var mySensors = new SensorCollection();
mySensors.AddSensor(new Sensor("", "123", "Home"));
mySensors.AddSensor(new Sensor("", "123", "Work"));

foreach(Sensor s in mySensors.Sensors)

I can not seem to access the members of the class in another file

Make sure they're in the same namespace, or that you include a "using" statement that includes the namespace of your classes you create.

2012-04-04 19:22
by Jesse
Thank you for your responses. I have a variable number of sensors. I dynamically create sub tabs for each sensor. I want to have a class that will hold the three string members (ipaddr, portnum, loc). These members are populated via an .ini file. The members need to be accessed in other files and that is where I am getting hung up.E.g. How do I access the ipaddress of each sensor in my dynamically created list of classes - user1313577 2012-04-04 21:02
Let me clarify my previous post. I want a dynamic array of the sensor class. In the first file I was able to access each member by using this: (SensorArrayList[0] as SensorDataCollection).ipAddress but I can not seem to access the members of the class in another file. SensorArrayList[i - user1313577 2012-04-04 21:07
I edited my answer a bit. See if that helps. Granted, there's several different ways you could go about your particular goals (such as creating your own custom collection that implements IList, or simply using List and not using a custom collection class at all) so you may want to consider those alternatives - Jesse 2012-04-04 21:29
I have implemented your suggestions above and ca - user1313577 2012-04-04 21:45
I have implemented your suggestions above and can fill my list of sensors. Now when I go to a separate file, mainForm.cs I am trying to access members of this array of sensors. AdvancedSettingsForm.DetectorCollection myMainFormDetectorCollection = new AdvancedSettingsForm.DetectorCollection(); but the class is not visible with this instantiation - user1313577 2012-04-04 21:53
string x2 = myDetectors[0].Ip; This works in the file where the classes are declared but it doesn't work in a separate file. I declare the class AdvancedSettingsForm.Detector myMainFormDetectors; AdvancedSettingsForm.DetectorCollection myMainFormDetectorCollection = new AdvancedSettingsForm.DetectorCollection(); string x2 = myMainFormDetectors[0].Ip; this doesn't work. It won't allow indexing on thi - user1313577 2012-04-04 22:18
I was using the wrong class in the mainForm.cs file. It will compile if I use string x2 = myMainFormDetectorCollection[0].Ip; however it is empty and I know it is getting filled in the sensor.cs file - user1313577 2012-04-04 22:47