我有一個重載構造函數的類。我想對代碼進行重新分解,以便對構造函數的一種形式的調用將參數轉換爲另一種構造函數接受的格式,然後調用它。如何在同一對象的構造函數中調用不同的構造函數?
這裏是我目前擁有的三個構造函數的代碼:
/// <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();
}
正如你可以看到有重複代碼的前兩個構造函數,因此重新分解。
我知道我可以有一個確實的對象初始化的方法和調用這個從每個構造函數,但是這似乎並不很優雅。
任何人都可以點我在正確的方向? 謝謝。
你要離開'm_DSList'未初始化(或任何它被宣佈爲)如果前兩個構造函數接收空? – BoltClock
** CreateTabPages()**方法將處理m_DSList爲空,但很好,你發現它。我會相應地評論我的代碼。 – TeamWild