Fetch a value of a property of an object in arraylist (C#) -



Fetch a value of a property of an object in arraylist (C#) -

i have initialised arraylist in c# windows form application. adding new objects few properties of each object in arraylist, such as:

arraylist formfields = new arraylist(); cdatabasefield db = new cdatabasefield(); db.fieldname = fieldname; //fieldname input value fetched windows form db.pageno = pageno; //pageno, description, buttoncommand fetched fieldname db.description = description; db.buttoncommand = buttoncommand; formfields.add(db);

now when want check fieldname of each object in arraylist (suppose there many objects in arraylist). how can that??

i tried:

for(int i=0; i<formfields.count; i++) { fieldname = formfields[i].fieldname; }

but generating error (in ide). new c# programming, can help me this??

error: error 21 'object' not contain definition 'fieldname' , no extension method 'fieldname' accepting first argument of type 'object' found (are missing using directive or assembly reference?)

arraylist holds objects. it's not generic , type safe.that's why need cast object access it's properties. instead consider using generic collections list<t>.

var formfields = new list<cdatabasefield>(); cdatabasefield db = new cdatabasefield(); ... formfields.add(db);

then can see properties visible because compiler knows type of elements , allows access members of type in type-safe manner.

c# arraylist

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -