7

I have the following set up:

public abstract class A
{
 public void f()
 {
 //Want to make an instance of B or C here
 //A bOrC = new ?
 }
 public abstract void f2();
}
public class B : A { public override void f2(){} }
public class C : A { public override void f2(){} }

Is this possible? If so how?

Edit: bOrC needs to be the type of the particular derived class f() is called from

asked Feb 27, 2011 at 19:10
6
  • 2
    Sure, you can new B() or new C() - is there something specific in your context that makes this not possible? Commented Feb 27, 2011 at 19:11
  • 1
    I think the point is that an instance of B or C is required, but which of these types is not known at the point of the call. Commented Feb 27, 2011 at 19:14
  • Is the f() method to make a copy of the current instance or something - ie to make a new instance of the child type? Commented Feb 27, 2011 at 19:16
  • can you be a bit more specific? Commented Feb 27, 2011 at 19:17
  • Perhaps this helps: stackoverflow.com/questions/1368643/… Commented Feb 27, 2011 at 19:21

3 Answers 3

9

I can think of two ways to solve this issue. One uses generics and the other just requires an abstract method. First the simple one.

public abstract class A
{
 public void f()
 {
 A bOrC = newInstance();
 }
 public abstract void f2();
 protected abstract A newInstance();
}
public class B : A {
 public override void f2(){}
 public override A newInstance(){
 return new B();
 }
}
public class C : A {
 public override void f2(){}
 public override A newInstance(){
 return new C();
 }
}

And now with generics

public abstract class A<T> where T : A, new()
{
 public void f()
 {
 A bOrC = new T();
 }
 public abstract void f2();
}
public class B : A<B> {
 public override void f2(){}
}
public class C : A<C> {
 public override void f2(){}
}
answered Feb 27, 2011 at 19:17
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I had thought of your first method, but wondered if there was a neater way of doing it using things like this.GetType()
4

You can use Activator.CreateInstance(this.GetType());

answered Feb 27, 2011 at 19:17

2 Comments

It wasn't me that down voted, but I would then need to cast the object it returns into B or C without knowing which one. It doesn't solve my problem
You can just cast it to A, and you'll have everything you would be able to do the other way as well.
0

This is not possible, and would lead to some weird consequences if it was. However, there is an easy work around rendering code that is easy to read.

public abstract class A
{
 public void f()
 {
 //Want to make an instance of B or C here
 //A bOrC = new ?
 A bOrC = Create();
 }
 public abstract void f2();
 public abstract A Create();
}
public class B : A {
 public override void f2(){}
 public override A Create() { return new B(); }
}
public class C : A {
 public override void f2(){}
 public override A Create() { return new C(); }
}
answered Feb 27, 2011 at 19:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.