【问题标题】:How do I call a different constructor from within a constructor in the same object?如何从同一对象的构造函数中调用不同的构造函数?
【发布时间】:2011-12-15 15:49:50
【问题描述】:

我有一个具有重载构造函数的类。我想重构代码,以便对构造函数的一种形式的调用将参数转换为另一个构造函数接受的格式,然后调用它。

这是我目前拥有的三个构造函数的代码:

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

如您所见,前两个构造函数中有重复代码,因此需要重构。

我知道我可以有一个方法来初始化对象并从每个构造函数中调用它,但这似乎不是很优雅。

谁能指出我正确的方向? 谢谢。

【问题讨论】:

  • 如果前两个构造函数接收到 null,您是否要让 m_DSList 未初始化(或声明的任何内容)?
  • CreateTabPages() 方法将处理 m_DSList 为空,但您发现它很好。我会相应地评论我的代码。

标签: c# constructor


【解决方案1】:
  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()
         {
         }

【讨论】:

    【解决方案2】:

    试试这个

    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。是的,添加 this(DataArray != null ? DataArray.ToList() : new List(System.Data.DataSet&gt;()) 之类的内容很简单
    • @Stecya 这正是我所追求的。谢谢
    • 只有1个查询,我觉得第二个构造函数应该是public DataForm(DataSet[] DataArray): this(new List(DataArray)){} 因为 DataSet[] 没有实现 ToList()
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2023-04-03
    • 2012-06-22
    • 2019-02-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多