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.
file1
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;
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):
mySCA.SensorArrayList.Add(mySensorCollection);
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
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.
//Sensor.cs
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;
}
}
//SensorCollection.cs
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()
{
this.sensors.Add(sensor);
}
public void AddSensor(Sensor sensor)
{
//Determine whether or not to add it
this.sensors.Add(sensor);
}
public void RemoveSensor(Sensor sensor)
{
if (sensors.Contains(sensor))
sensors.Remove(sensor);
}
}
Edit
How do I access the ipaddress of each sensor in my dynamically created list of classes?
var mySensors = new SensorCollection();
mySensors.AddSensor(new Sensor("1.1.1.1", "123", "Home"));
mySensors.AddSensor(new Sensor("9.9.9.9", "123", "Work"));
foreach(Sensor s in mySensors.Sensors)
Console.WriteLine(s.Ip);
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.
DetectorCollection
but your class isSensorCollection
- Saeed Amiri 2012-04-04 18:46