Can’t figure this one out
I have an ArrayList of classes:
// Holds an image
public class productImage
{
public int imageID;
public string imageURL;
public DateTime dateAdded;
public string slideTitle;
public string slideDescrip;
}
public ArrayList productImages = new ArrayList();
productImage newImage = new productImage();
newImage.imageID = 123;
productImages.Add(newImage);
Now how do I access the property?
int something = productImages[0].imageID
Doesn’t work!
Error 1 ‘object’ does not contain a
definition for ‘slideTitle’ and no
extension method ‘slideTitle’
accepting a first argument of type
‘object’ could be found (are you
missing a using directive or an
assembly reference?)
The values in an
ArrayListare typed toObject. You need to cast toproductImageto access the property.A much better solution though is to used a strongly typed collection like
List<T>. You can specify the element type isproductImageand avoid the casting altogether.