Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone.
Let's supose i have the following classes:

class ClBase{

 public ClBase(){/*do stuff..*/}

 protected postConstructorMethod(){ /*do do more stuff..*/}

}

class ClInherited:ClBase{

 public ClInherited():base(){/*do stuff..*/}

}


Is there any way to call "postConstructorMethod()" after the derived class constructor?

Thanks in advance
Posted
Comments
Adrián Cuéllar 22-Jan-15 9:25am    
This solved my problem, thanks Nathan Minier


class ClInherited:ClBase{

protected ClInherited():base(){/*do stuff..*/}

public static ClInherited Initialize()
{
var output = new ClInherited();
output.postConstructorMethod();
return output;
}
}


Nathan Minier 22-Jan-15 9:49am    
I'm glad to hear it, it's a variation on the Singleton pattern. You'll want to make sure to make your base class constructor protected as well to enforce the Initialize pattern. There are things that you can do to touch it up a bit as well, but those would work best if you implemented an interface for your ancestor-child chain and used that as your Initialize return type; then you could put Initialize on the base class and not worry about implementing it on your children.

You can call any public or protected method of the base class as if they were native methods.

If you have a virtual method, you can call the base version using the base.Method() syntax. You see this quite often in Dispose methods.

C#
protected override void Dispose(bool disposing)
{
    if(!_disposed)
    {
        if(disposing)
        {
            // dispose managed resources
           base.Dispose(true);
        }
    }
}
 
Share this answer
 
Comments
Adrián Cuéllar 22-Jan-15 8:48am    
The issue is that the stuff i need to do in the "postConstructorMethod()" is always the same for all the derived classes.
Nathan Minier 22-Jan-15 8:52am    
That's fine, then just call it in the constructor of your derived classes:

class ClInherited:ClBase{

public ClInherited():base()
{
/*do stuff..*/
postConstructorMethod();
}
}
or call it after the fact, whatever design pattern you happen to require.

Nathan Minier 22-Jan-15 9:02am    
Is your issue that you need an Initialize type of method? Something like:

class ClInherited:ClBase{

protected ClInherited():base(){/*do stuff..*/}

public static ClInherited Initialize()
{
var output = new ClInherited();
output.postConstructorMethod();
return output;
}
}
I'm not so sure you really need a Singleton here, or the use of 'static:
C#
public class RequireMethodInCtor
{
    public string Name { set; get; }
    public Guid Id { private set; get; }

    public RequireMethodInCtor(string name)
    {
        Name = name;
        Id = Guid.NewGuid();
        MethodInCtor(this);
    }

    protected void MethodInCtor(RequireMethodInCtor instance)
    {
        Console.WriteLine("New instance: {0} Id: {1}", instance.Name, instance.Id.ToString());
    }
}

public class Derived1 : RequireMethodInCtor
{
    public string SomeString { set; get; }

    // Name and Id fields will be initialized by the call to 'base here
    public Derived1(string name, string somestring) : base(name)
    {
        SomeString = somestring;
    }
}
Create a new instance of 'Derived1, and put a breakpoint in your code before it is executed: when you hit the breakpoint, single-step (F11) and observe what happens.

If you absolutely must call a method in the base Class after the derived class 'ctor executes whatever initial code:
C#
public class RequireMethodInCtor
{
    public string Name { set; get; }
    public Guid Id { private set; get; }

    public RequireMethodInCtor(string name)
    {
        Name = name;

        Id = Guid.NewGuid();
    }

    protected void MethodInCtor(RequireMethodInCtor instance)
    {
        Console.WriteLine("New instance: {0} Id: {1}", instance.Name, instance.Id.ToString());
    }
}

public class Derived1 : RequireMethodInCtor
{
    public string SomeString { set; get; }

    public Derived1(string name, string somestring) : base(name)
    {
        Name = name;
        SomeString = somestring;

        // now call the base method
        base.MethodInCtor(this);
    }
}
 
Share this answer
 
v3
Comments
Nathan Minier 27-Jan-15 9:04am    
It's not really a singleton, since were not tracking the instance, thread locking, etc. My primary reason for not suggesting the pattern that you posted was that he specifically asked for a method to take place after construction, which infers (to me) that he'll be acting on/with the instance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900