TeamWild
TeamWild

Reputation: 2550

How do I call a different constructor from within a constructor in the same object?

I have a class that has overloaded constructors. I'd like to re-factor the code so that a call to one form of the constructor will convert the parameters into a format that is accepted by another constructor and then call that.

Here is the code for the three constructors that I currently have:

/// <summary>
/// Original Constructor accepting a single dataset
/// </summary>
/// <param name="theData"> The dataset containing the tables to be displayed</param>    
public DataForm(System.Data.DataSet theData)
{
    InitializeComponent();

    // Ensure a DataSets has been passed for display
    if (theData != null)
    {
        // Create a new list and add the single dataset to it
        List<DataSet> tempDataList = new List<DataSet>();
        tempDataList.Add(theData);

        // Assign the list of datasets to the member variable
        this.m_DSList = tempDataList;
    }

    CreateTabPages();
}

/// <summary>
/// Construct the form with tab pages assigned for each dataset
/// </summary>
/// <param name="DataArray">A collection of datasets, each to be displayed on their own tab page</param>
public DataForm(DataSet[] DataArray)
{
    InitializeComponent();

    // Ensure some DataSets have been passed for display
    if (DataArray != null && DataArray.Length > 0)
    {
        // Assign the list of datasets to teh member variable
        this.m_DSList = new List<DataSet>(DataArray);
    }

    CreateTabPages();
}

/// <summary>
/// Construct the form with tab pages assigned for each dataset
/// </summary>
/// <param name="DataList">A collection of datasets, each displayed on their own tab page</param>
public DataForm( List<System.Data.DataSet> DataList )
{
    InitializeComponent();

    // Assign the list of datasets to teh member variable
    this.m_DSList = DataList;

    CreateTabPages();
}

As you can see there is repeated code in the first two constructors, hence the re-factoring.

I know I could have a method that does the initialisation of the object and call this from each of the constructors but this doesn't seem very elegant.

Can anyone point me in the right direction? Thanks.

Upvotes: 3

Views: 238

Answers (2)

s_nair
s_nair

Reputation: 812

  public DataForm()
        {
             InitializeComponent(); 
        }
        public DataForm(DataSet theData): this()
        {
            this.m_DSList= (theData!=null) ? new List<DataSet>{theData}:null;
        }
        public DataForm(DataSet[] DataArray):this()
        {
             this.m_DSList= (DataArray!=null && DataArray.Length > 0) ? new List<DataSet>(DataArray):null;
        }
        public DataForm(List<DataSet> DataList ):this() 
        {           
            this.m_DSList = DataList; 
        }
        //Take this method out from constructor and for a better design but not mandatory
         public void CreateTabPages()
         {
         }

Upvotes: 0

Stecya
Stecya

Reputation: 23266

Try this

public DataForm(System.Data.DataSet theData): this(new List<System.Data.DataSet>{theData}){}

public DataForm(DataSet[] DataArray): this(DataArray.ToList()){}

public DataForm( List<System.Data.DataSet> DataList )
{
    InitializeComponent();

    // Assign the list of datasets to teh member variable
    this.m_DSList = DataList;

    CreateTabPages();
}

Upvotes: 6

Related Questions