Waheed
Waheed

Reputation: 10206

Inheriting Constructor. i.e. calling an other function of same class from constructor

I am calling function of the same class from constructor. But its giving me the error.

public class Connection
{
    public Connection()
    {
    }

    public Connection(string parameter) : this(GetConnection(parameter))
    {
    }

    private static string GetConnection(string parameter)
    {
        return parameter;
    }
}

But public Connection(string parameter) : this(GetConnection(parameter)) is giving me error. The error is:

Constructor 'Test.Connection.Connection(string)' cannot call itself.

What is the error behind this. Is this type of calling possible??

Thanks!!!

`

Upvotes: 0

Views: 1001

Answers (3)

Adil
Adil

Reputation: 148120

You are recursively calling contructor with one parameter of type string. You have two constructors one is parmeterless and other with one parameter, The constuctor from which you are calling this(GetConnection(parameter)) is the only constructor with one parameter and compiler will again call the constructor from which you are calling instead of calling parameterless construtor (the other constructor you have).

This will call parameterless constructor from one parameter constructor.

public Connection()
{
}

public Connection(string parameter) : this()
{
}

To call constructor of base class you need to use :base() instead of this()

Upvotes: 1

horgh
horgh

Reputation: 18534

You can call another constructor with this syntax (either this class constructor through this keyword, or one of the available base class constructors through base keyword). While you're having a potential StackOverflowException here instead.

You can simply do this:

public class Connection
{
    public Connection()
    {
    }

    public Connection(string parameter)
        : this()
    {
        string connectionString = GetConnection(parameter);
    }
}

Off topic: the following potential StackOverflowException is already not identifiable by compiler (i.e. it's compiled without errors and warnings), but only at runtime:

public class Connection
{
    public Connection()
        : this(GetConnectionString())
    {
    }

    public Connection(string parameter)
        : this()
    {
    }

    public static string GetConnectionString()
    {
        //...
    }
}

Please read Using Constructors (C# Programming Guide) for more info:

A constructor can invoke another constructor in the same object using the this keyword. Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this, or as part of an expression.

Also see Calling base constructor in c#.

Upvotes: 2

dutzu
dutzu

Reputation: 3910

Why not just call it from within the Constructor's scope?

You can have something like this maybe:

public Connection(bool getParam = false)
    {
       if (getParam)
       {
          _param = GetConnection(parameter);
       }
    }

Upvotes: 0

Related Questions