Collection of Classes --

Hi

I have a two classes which are inherited from one class. I want to make collection of all derived classes in one list.
And when i enumrate thro that collection i should able to call correct method of that class.

For ex --

class Bbase
{
   // public member, method ....
   public virtual void Method1()
    {
    }
}

class derived1 : Bbase
{
   public new void Method1()
   {
   }
   public void Method1_1()
   {
   }
}


class derived1 : Bbase
{
   public new void Method1()
   {
   }
   public void Method2_1()
   {
   }
}



// Main function is something like that ...
Main()
{
   ArrayList  oArray;
   derived1  ob1, ob2, ob3;
   derived2   o1, o2;
   int i;

   Bbase oBase;
   oArray = new ArrayList();
   
  // Added all these object in collection
   ob1 = new derived1();
   ob2 = new derived1();
   ob3 = new derived1();
   o1 = new derived2();
   o2 = new derived2();
   oArray.Add(ob1);
   oArray.Add(ob2);
   oArray.Add(ob3);
   oArray.Add(o1);
   oArray.Add(o2);
 

  // Interate thro array.
  for ( i = 0; i < oArray.Count; i ++)
  {
     oBase = (Bbase)oArray[i];
     oBase.Method1();  // Always calls Base - method1.

    // To make it correct

   if (oArray[i].GetType().Name == "derived1")
        oDerived1 = (dervied1)oArray[i];
        oDervied1.Method1();
   else if (oArray[i].GetType().Name == "derived2")
        oDerived2 = (dervied2)oArray[i];
        oDervied2.Method1();
   
  }
}

In above code , i type cast the array object. I want to do that without typecasting .... Is it possible ??

Regards & Thanks.


Rajesh_Patil74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Replace new with override:

class Bbase
{
   public virtual void Method1()
   ...
}

class derived1 : Bbase
{
   public override void Method1()
   ...
}

class derived2 : Bbase
{
   public override void Method1()
   ...
}

In this case the following code calls right function:


 for ( i = 0; i < oArray.Count; i ++)
  {
     oBase = (Bbase)oArray[i];
     oBase.Method1();
  }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
muzzy2003Commented:
If you specify override and not new for the Method1 declarations in the inheriting objects, then oBase.Method1() should call the method you want it to.
0
tstoeckigtCommented:
For enumerating through an [collection|list|array] you should use the foreach keyword:

foreach(Bbase oBase in oArray)
  oBase.Method1();

This version looks much cleaner.

Your classes should also use the keyword override as AlexFM and muzzy2003 already mentioned.

Ciao
Timo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.