0

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!!!

`

horgh
  • 17,918
  • 22
  • 68
  • 123
Waheed
  • 10,086
  • 20
  • 53
  • 66
  • Cant you move the call to within the constructor? The usage you have is specific to constructors not methods. – Ravi Y Dec 20 '12 at 07:33
  • Yes it can be done in that way also. But I have seen this type of code from Microsoft. So I was trying in this way. – Waheed Dec 20 '12 at 07:36
  • It can't be done because you're creating an infinite loop. 'this' on your second constructor is calling your second constructor again and then this calling your second constructor again ... You need to move 'this' to your default constructor and provide a default value there. – thomasvdb Dec 20 '12 at 07:38
  • Not possible to call recursively.. anyway what are you trying to achieve with that? – indiPy Dec 20 '12 at 07:40
  • This is the code from Microsoft. public CrmConnection(string connectionStringName) : this(GetConnectionStringSettings(connectionStringName)) { } – Waheed Dec 20 '12 at 07:42
  • How they have done this??? – Waheed Dec 20 '12 at 07:42
  • GetConnectionStringSettings(connectionStringName) might not returning same type as method name says "settings" so there must be overloaded constructor. – indiPy Dec 20 '12 at 07:46
  • NO its returning. This function is as: private static ConnectionStringSettings GetConnectionStringSettings(string connectionStringName) { – Waheed Dec 20 '12 at 08:06
  • @Waheed the constructor getting called with a string parameter is calling a constructor with a ConnectionStringSettings parameter, ie a different constructor. – Destrictor Dec 20 '12 at 08:16

3 Answers3

2

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#.

Community
  • 1
  • 1
horgh
  • 17,918
  • 22
  • 68
  • 123
  • Thanks @Konstantin Vasilcov. But how this code is working? public CrmConnection(ConnectionStringSettings connectionString) : this(CrmConfigurationManager.CreateConnectionDictionary(connectionString)) { } AND private static ConnectionStringSettings GetConnectionStringSettings(string connectionStringName) { This code is working. How this??? You are also correct. May be may be this is dump code...... I have taken this from microsoft's dll. – Waheed Dec 20 '12 at 08:20
  • @Waheed In each constructor declaration, you have an opportunity to call any of other available (this class or base class) constructors. Surely if the called constructor need any argument, you must specify them. That's it. The only thing that is wrong about your code is that you called a constructor from its declaration. That is a 100% stack overflow (as simple that a constructor can recognise it) – horgh Dec 20 '12 at 08:29
1

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()

Adil
  • 146,340
  • 25
  • 209
  • 204
0

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);
       }
    }
dutzu
  • 3,883
  • 13
  • 19
  • Thanks @dutzu. Yes it can be done in that way also. But I have seen this type of code from Microsoft. So I was trying by this method. – Waheed Dec 20 '12 at 07:38