3

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.

2
  • Do you want to leave m_DSList uninitialized (or whatever it was declared as) if the first two constructors receive null? Commented Oct 26, 2011 at 11:27
  • The CreateTabPages() method will handle a m_DSList being null but it's good that you spotted it. I will comment my code accordingly. Commented Oct 26, 2011 at 12:51

2 Answers 2

6

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

What if the parameters to the first two constructors are null?
You can check in third constructor whether list is null or contains null elements. But best solution is to validate those parameters before creation
+1. And yeah, it's simple enough to add things like, this(DataArray != null ? DataArray.ToList() : new List(System.Data.DataSet>())
@Stecya This is exactly what I was after. Thanks
Just 1 query, I think the second constructor should be public DataForm(DataSet[] DataArray): this(new List<System.Data.DataSet>(DataArray)){} as the DataSet[] doesn't implement ToList()
|
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()
         {
         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.