简体   繁体   中英

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.

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();
}
  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()
         {
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM