2011-10-26 74 views
3

我有一個重載構造函數的類。我想對代碼進行重新分解,以便對構造函數的一種形式的調用將參數轉換爲另一種構造函數接受的格式,然後調用它。如何在同一對象的構造函數中調用不同的構造函數?

這裏是我目前擁有的三個構造函數的代碼:

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

正如你可以看到有重複代碼的前兩個構造函數,因此重新分解。

我知道我可以有一個確實的對象初始化的方法和調用這個從每個構造函數,但是這似乎並不很優雅。

任何人都可以點我在正確的方向? 謝謝。

+0

你要離開'm_DSList'未初始化(或任何它被宣佈爲)如果前兩個構造函數接收空? – BoltClock

+0

** CreateTabPages()**方法將處理m_DSList爲空,但很好,你發現它。我會相應地評論我的代碼。 – TeamWild

回答

6

試試這個

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

如果前兩個構造函數的參數爲​​空,該怎麼辦? – BoltClock

+0

您可以檢查第三個構造函數列表是否爲空或包含空元素。但最好的解決方案是在創建之前驗證這些參數 – Stecya

+0

+1。 ,是的,這是很簡單的添加喜歡的東西,這'(於DataArray = NULL DataArray.ToList():?新的List(System.Data.DataSet中>())' –

0
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() 
     { 
     } 
相關問題