-2

In a situation where I have two classes, abstract A, and B : A,

Is it possible to have a method within A, that implicitly returns a new instance of B? By implicitly, I mean that the type of B is never actually specified in code, but implied from the context. In code terms,

abstract class A 
{
   A New() => return new this; //this isnt valid but illustrates desired functionality
}
class SomeTypeOfDerivedClass1 : A 
{}
class SomeTypeOfDerivedClass2 : A 
{}

usage would be similar to this:

var someType = new SomeTypeOfDerivedClass1();
var someType2 = new SomeTypeOfDerivedClass2();

var newInstance = someType2.New();  // assigned new instance of SomeTypeOfDerivedClass2
newInstance = someType1.New();  // assigned new instance of SomeTypeOfDerivedClass1
cubesnyc
  • 1,385
  • 2
  • 15
  • 32
  • So you want to return a derived class filled with the information in your current base class? – TheGeneral Jul 31 '19 at 03:02
  • not sure what you mean by information, but yes, return a new instance of the derived class from within the base class. – cubesnyc Jul 31 '19 at 03:07
  • By information, i mean data... Properties, State ect – TheGeneral Jul 31 '19 at 03:08
  • There are already _lots_ of examples of this on Stack Overflow. You can use `this.GetType()` to get the `Type` reference, which is then created via `Activator` per the marked duplicate. – Peter Duniho Jul 31 '19 at 05:14
  • `var bob = SomeTypeOfDerivedClass1.New();` What should the type of the `bob` variable be (i.e. if you hover over `var` what do you expect to see)? – mjwills Jul 31 '19 at 05:15

3 Answers3

0

This is seems more than a little suspect, however in its simplest form

public B CreateNewInstanceOfB()
{
    return new B()
      {
          //Fill out your properties here
      };
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

the more complete one colud be the following code

public abstract class  A
{
    public  int Prop1 { get; set; }
    public B GetB()
    {
        return new B(){Prop1 = 10, Prop2 = 20};
    }
}

public class B : A
{
    public int Prop2 { get; set; }
}
0

Here goes:

public abstract class ParentClass
{
  /* virtual */ ParentClass /* func */ SomeOperation ( )
  {
     return null;
  }

 /* virtual */ void /* func */ SayHello ( )
 {
   // output "hello from ParentClass";
 }
} // class

public /* concrete */ class ChildClass : 
  /* extends */ ParentClass
{
   /* override */ ParentClass /* func */ SomeOperation ( )
   {
      ChildClass /* var */ Result = new ChildClass ( );
      // assign properties here
      return Result;
   }

  /* override */ void /* func */ SayHello ( )
  {
     // output "hello from ChildClass"
  }
} // class

public /* concrete */ class Demo
{
   void /* func */ SomeDemo ( )
   {
      ParentClass /* var*/ MyObject =
         new ChildClass ( );

      ParentClass /* var */ AnotherObject = 
         MyObject.SomeOperation( );

      AnotherObject.SayHello( );
   }
}  // class

Please ignore smaller bugs, and particular way of coding.

umlcat
  • 4,091
  • 3
  • 19
  • 29