none
how to use initializers to call other constructors

    Question

  • 1   public BrokerConnector(string host, int port, int clientId, bool scan, bool monPortfolio, BackgroundWorker worker)  
    2         {  
    3             this.worker = worker;  
    4             BrokerConnector(host, port, clientId, scan, monPortfolio);  
    5         }  
    6  
    7         public BrokerConnector(string host, int port, int clientId,bool scan, bool monPortfolio)  
    8         {  
    9           // do something.  
    10         } 
    i read that in c# you cannot use call 1 overloaded constructor from another. so what are the other options available. 
    Wednesday, December 31, 2008 3:31 PM

Answers

  • public BrokerConnector(string host, int port, int clientId, bool scan, bool monPortfolio, BackgroundWorker worker) 
                     : this(host, port, clientId, scan, monPortfolio)   
    2           {   
    3               this.worker = worker;   
    5           }   
    6     
    7           public BrokerConnector(string host, int port, int clientId,bool scan, bool monPortfolio)   
    8           {   
    9             // do something.   
    10          }  

    • Marked as answer by Murthy Suresh Wednesday, December 31, 2008 3:39 PM
    Wednesday, December 31, 2008 3:37 PM

All replies

  • public BrokerConnector(string host, int port, int clientId, bool scan, bool monPortfolio, BackgroundWorker worker) 
                     : this(host, port, clientId, scan, monPortfolio)   
    2           {   
    3               this.worker = worker;   
    5           }   
    6     
    7           public BrokerConnector(string host, int port, int clientId,bool scan, bool monPortfolio)   
    8           {   
    9             // do something.   
    10          }  

    • Marked as answer by Murthy Suresh Wednesday, December 31, 2008 3:39 PM
    Wednesday, December 31, 2008 3:37 PM
  • Hi dilibu,
    you can call another constructor... but not the way you are doing it right now. If you aren't accessing this.worker in your other constructor, you could just write:

    public BrokerConnector (string host, int port, int clientId, bool scan, bool monPortfolio, BackgroundWorker worker)
      : this (host, port, clientId, scan, monPortfolio)

    {
      this.worker = worker;
    }

    Anyway, I usually prefer to do it the other way around... i.e. implement only the constructor with all the parameters and then provide alternate constructor that specify default values for the missing parameters. This way, the construction logic is all grouped together. In your case:

    public BrokerConnector (string host, int port, int clientId, bool scan, bool monPortfolio, BackgroundWorker worker) {
      this.worker = worker;
      // do something
    }

    public BrokerConnector (string host, int port, int clientId, bool scan, bool monPortfolio)
      : this (host, port, clientId, scan, monPortfolio, null) // I assume that null is a reasonable default for this.worker...
    {
    }

    HTH
    --mc

     
    Wednesday, December 31, 2008 3:49 PM