Skip to content

Problem with ArrayList storing objects of different class type in c#

Giftsana

Asked Feb 22, 2006, 4:23 PM EST2 answers
Share
LinkedInX

I have an ArrayList that has objects of different class types stored in it. Basically each of thess class represent a different Questionnaire class.

I retrieve each of these Questionnaire class with it's specific info like 'Question ID', 'Question text', 'Number of Choices' etc. and store it in an ArrayList say arrQDetails.

Now i want to iterate through each of the question objects in the ArrayList and perform respective implementations in each object i.e dynamically generating checkboxes based on 'Number of Choice'.

i am using foreach(Object o in objQuestionnaire.arrQDetails) . but it doesnt seem to work?if there are 2 objects(each have different details) of the same class type, i am able to dynamically generate controls only for one object type not the second object?

The arraylist may also have more than 1 object of the same type type but each with different info. there are 4 different class type of objects that am storing.

How do i solve this?

Awaiting a response soon. Thanks in advance.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    You probably want to use your own base class for the questions instead of using object as the base class.

    Do something along the lines of:
    class QuestionType
    {
    // methods that are generic to all your classes
    public void doSomething() {  }
    }

    then another class
    class SpecificQuestionType : QuestionType
    {
    // override your generic method
    override public void doSomething() { // more specific stuff }
    }


    Then your foreach loop would be more like:
    foreach(QuestionType qt in QuestionList)
       qt.doSomething();


    Hope this helps...

    0
  • 0
    Thanks a lot for the reply. I got a solution to it.

    Thanks
    Giftsana
    0