Somdip Dey
Somdip Dey

Reputation: 3386

Fetch a value of a property of an object in ArrayList

I have initialized an ArrayList in C# for windows form application. I am adding new objects with few properties of each object in the ArrayList, such as:

ArrayList FormFields = new ArrayList();

CDatabaseField Db = new CDatabaseField();
Db.FieldName = FieldName; //FieldName is the input value fetched from the Windows Form
Db.PageNo = PageNo; //PageNo, Description, ButtonCommand are also fetched like FieldName
Db.Description = Description;
Db.ButtonCommand = ButtonCommand;
FormFields.Add(Db);

Now When I want to check only the FieldName of each object in the ArrayList (suppose there are many objects in the ArrayList). How can I do that??

I tried:

for(int i=0; i<FormFields.Count; i++) 
{
    FieldName = FormFields[i].FieldName;
}

But this is generating error (in the IDE). I am new to C# programming, so can someone help me with this??

Error: Error 21 'object' does not contain a definition for 'FieldName' and no extension method 'FieldName' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 4

Views: 24264

Answers (3)

Salah Akbari
Salah Akbari

Reputation: 39956

As already pointed and based on this:

The Item returns an Object, so you may need to cast the returned value to the original type in order to manipulate it. It is important to note that ArrayList is not a strongly-typed collection. For a strongly-typed alternative, see List<T>.

However as an another option you can use the foreach loop instead of for. When foreach runs, it tries to cast element of ArrayList to CDatabaseField and if an element is not convertible to the CDatabaseField you will get an InvalidCastException:

foreach (CDatabaseField item in FormFields)
{
    FieldName = item.FieldName;
}

According to foreach documentation and C#6 syntax the above code is equivalent to this:

var enumerator = FormFields.GetEnumerator();
try
{
    while (enumerator.MoveNext())
    {
        CDatabaseField item = (CDatabaseField)enumerator.Current;
    }
}
finally
{
    var disposable = enumerator as IDisposable;
    disposable?.Dispose();
}

Upvotes: 1

Somdip Dey
Somdip Dey

Reputation: 3386

Finally figured out the answer. I tried to cast the objects for each object saved in the arraylist and finally could fetch the required field of each object:

for (int i = 0; i < FormFields.Count; i++)
{
     CDatabaseField Db = (CDatabaseField)FormFields[i];
     Label1.Text = Db.FieldName; //FieldName is the required property to fetch
}

Upvotes: 4

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

ArrayList holds objects. It's not generic and type safe.That's why you need to cast your object to access it's properties. Instead consider using generic collections like List<T>.

var FormFields = new List<CDatabaseField>();
CDatabaseField Db = new CDatabaseField();
...
FormFields.Add(Db);

Then you can see that all properties will be visible because now compiler knows the type of your elements and allows you to access members of your type in a type-safe manner.

Upvotes: 5

Related Questions