简体   繁体   中英

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 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(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.

它知道类型,只需传递参数即可。

: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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM