Can constructor chaining be used in place of overloaded constructor
I have a constructor in my windows forms
public frmSecondarySEC(int intPrimaryDocumentId, int intSecondaryDocumentId, string strStateCode, int intCountyId, string strPrimaryDocTypeName, string strPrimaryDocTypeCode, string strDealName, string strLoanNumber, int intDealId, string strDealType, string strCountyName, bool blIsFileReview)
{
}
Now i need to pass an extra parameter to this constructor. For this i created an overloaded version of this constructor so that other classes using the original constructor does not break up.Only difference is addition of new parameter.
public frmSecondarySEC(int intPrimaryDocumentId, int intSecondaryDocumentId, string strStateCode, int intCountyId, string strPrimaryDocTypeName, string strPrimaryDocTypeCode, string strDealName, string strLoanNumber, int intDealId, string strDealType, string strCountyName, bool blIsFileReview,string primaryDocumentTitle)
{
}
Now since they share commom initialization code, i tried constructor chaining
public frmSe开发者_运维问答condarySEC(int intPrimaryDocumentId, int intSecondaryDocumentId, string strStateCode, int intCountyId, string strPrimaryDocTypeName, string strPrimaryDocTypeCode, string strDealName, string strLoanNumber,int intDealId, string strDealType, string strCountyName, bool blIsFileReview) : this(int intPrimaryDocumentId, int intSecondaryDocumentId, string strStateCode, int intCountyId, string strPrimaryDocTypeName, string strPrimaryDocTypeCode, string strDealName, string strLoanNumber, int intDealId, string strDealType, string strCountyName, bool blIsFileReview)
{
}
However I am getting an error on this part
:this(int intPrimaryDocumentId
which says
Invalid expression term 'int' .Am i using a wrong implementation of constructor chaining or it can be done some other way. I want to prevent duplication of code. Please suggest.
It knows the type, just pass the parameter.
:this(intPrimaryDocumentId)
You can call one from the other like so:
public M (int k) { }
public M () : this(1) { }
So, you don't redefine the variable, you just pass it in as if you are calling a method.
When chaining constructors, you don't specify the types again. It should be:
public frmSecondarySEC(int intPrimaryDocumentId, int intSecondaryDocumentId, string strStateCode, int intCountyId, string strPrimaryDocTypeName, string strPrimaryDocTypeCode, string strDealName, string strLoanNumber,int intDealId, string strDealType, string strCountyName, bool blIsFileReview)
: this(intPrimaryDocumentId, intSecondaryDocumentId, strStateCode, intCountyId, strPrimaryDocTypeName, strPrimaryDocTypeCode, strDealName, strLoanNumber, intDealId, strDealType, strCountyName, blIsFileReview)
Also, you should probably consider whether all those parameters are necessary. Seriously.
精彩评论