Question

There are 3 classes A,B,C . Classes B and C inherit from A. Class B and C have a function - func1() ,A doesnt. I have a list<A> OB where every object in it is eaither B or C. I want to access func1 by OB[0].Func1(). How can i do this? Thank you!

Was it helpful?

Solution 2

Add the method to class A as virtual, and make B and C override it:

public class A
{
    public virtual void Foo()
    {

    }
}

public class B : A
{
    public override void Foo()
    {

    }
}

public class C : A
{
    public override void Foo()
    {

    }
}

OTHER TIPS

You're trying to call method func1 on class A, where A does not define it? You can't. You can make Func1 abstract within A if you want to do that.

abstract class A 
{
    public abstract Func1();
}

class B : A
{
    public override Func1()
    {
        MessageBox.Show("Hello World");
    }
}

class C : A
{
    public override Func1()
    {
        MessageBox.Show("Goodbye World");
    }
}

The fact that Func1 is abstract means that you can't instantiate A directly, but you can instantiate B.

var listOfA = new List<A>();

listOfA.Add(new B());
listOfA.Add(new C());

listOfA[0].Func1(); // hello world
listOfA[1].Func1(); // goodbye world

You could make Func1 defined as virtual in A instead of setting it as abstract, but I recommend you do not because this will introduce a reversed Refused Bequest design smell.

If it doesn't make sense put func1() or can't change class A you can create a Interface that has func1() and have just class B and C implement that interface. Then when you need to call func1() cast your objects to that interface using the as operator. When using the as operator no exception is thrown if the cast fails.

public interface MyInterface
{
    void func1();
}

public class B : MyInterface
{
    public func1() {...}
    ....
}

public class C : MyInterface
{
    public void func1() {...}
    ....
}

//example of calling func1()
List<A> list = new List<A>(stuff);
foreach(A item in list)
{
    MyInterface tmp = item as MyInterface;
    if(tmp != null)
    {
        tmp.func1();
    }
}

Abstract factory do the same thing you are looking for i found this code on CodeProject this might help you

//Let's define type of bread bases
public enum BreadBase
{
 HotIndianMasalaBase,
 PunjabiTadkaBase,
 ItalianCheeseBase,
 VeggieBase,
}
//This is a breadfactory where people visit to get their favorite bread bases
public interface BreadFactory
{
  Bread GetBread(BreadBase BreadBase);
}
 //The abstract bread
public interface Bread
{
   void Bake();
 }

//create concrete classes

 public class HotIndianMasalaBread :Bread
{
 public void Bake()
{ 
    Console.WriteLine ("For you::Hotindian Masala base Bread.");
}
}
public class VeggieBread : Bread
{
public void Bake()
{
    Console.WriteLine("For you::Veggie base Bread.");
}
}

 public class ItalianCheeseBread : Bread
{
public void Bake()
{
    Console.WriteLine("For you::Italian cheese base Bread.");
}
}
public class PunjabiTadkaBaseBread : Bread
{
 public void Bake()
{
    Console.WriteLine("For you::Punjabi tadka base bread.");
 }
}
//Lets create bread factories aka concrete classes

 public class AmericanBreadFactory :BreadFactory
 {
 public Bread GetBread(BreadBase BreadBase)
{
    Bread vBread = null;
    switch (BreadBase)
    {
        case BreadBase.VeggieBase:
            vBread = new VeggieBread();
            break;
        case BreadBase.ItalianCheeseBase:
            vBread = new ItalianCheeseBread();
            break;
    }
    return vBread;
  }
 } 

   public class IndianBreadFactory :BreadFactory
    {
 public Bread GetBread(BreadBase BreadBase)
{
    Bread vBread = null;
    switch (BreadBase)
    {
        case BreadBase.HotIndianMasalaBase:
            vBread = new HotIndianMasalaBread();
            break;
        case BreadBase.PunjabiTadkaBase:
            vBread = new PunjabiTadkaBaseBread();
            break;
    }
    return vBread;
   }
 }

//lets order breads

class Program
{
static void Main(string[] args)
{
//example of abstract factory
AmericanBreadFactory vAmericanBread = new AmericanBreadFactory();
Bread vBread = vAmericanBread.GetBread(BreadBase.VeggieBase);
vBread.Bake();

//lets bak indian punjabi tadka bread
IndianBreadFactory vIndianBreadFactory = new IndianBreadFactory();
Bread vIndianBread = vIndianBreadFactory.GetBread(BreadBase.PunjabiTadkaBase);
vIndianBread.Bake();
   }
 } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top