方法重载和参数6错误

时间:2015-07-01 14:42:44

标签: c# .net c#-4.0

我修改了我的代码和问题,以更好地反映我正在努力实现的目标。

背景:作为项目的一部分,我有不同的图层界面。

  • 服务层 - 处理我的业务逻辑,验证条目,(大脑)
  • 数据访问层 - 只需执行传递的方法或功能
  • Aspx&需要进行方法的aspx.cs文件(即用户界面)

这是我的ConnectionTypeSetup.aspx.cs文件的代码,我还标记了出错的行:

protected void uxSaveBtn_Click(object sender, EventArgs e)
        {
            var accountTrackersvc = new AccountTrackerSvc(); 

        //Insert or update record
        var result = ViewState["ConnectionTypeID"] == null ?
            accountTrackersvc.InsertConnectionType(uxConnectionTypeDescTxt.Text.Trim(),
                                            CommonSVC.GetUserInfoFormattedFromSession())

  /*Error on this line */                : accountTrackersvc.UpdateConnectionType(DataConverter.StringToInteger(ViewState["ConnectionTypeID"].ToString()),
                                           uxConnectionTypeDescTxt.Text.Trim(),
                                           Enums.GetIsDisabledByItemStatusValue(SafeValueAccessor.GetControlValue(uxStatusDdl)),
                                           CommonSVC.GetUserInfoFormattedFromSession(),"Default",false);


        //Check result
        if(result.Successful)
        {
             uxInfoMsg.DisplayMessage(result.Message, InfoMessage.InfoMessageType.Success);
             BindGridContent();
             uxPopupMdl.Hide();
        } 
        else
        {
            uxModalInfoMsg.DisplayMessage(result.Message, InfoMessage.InfoMessageType.Failure);
            uxPopupMdl.Show();
        }
        // Hide progress indicator
        Master.HideProgressIndicator();

再次处理我的业务逻辑的服务层格式如下。请注意,我们使用了两种不同的方法InsertUpdate

public BO.OperationResult InsertConnectionType(string connectionTypeDesc, string createdBy)
        {
            var operationResult = new BO.OperationResult();

        // connection type description required
        if (connectionTypeDesc.Trim().Length <= 0)
        {
            operationResult.Successful = false;
            operationResult.Message += "Connection type description is required";
        }
        //Createdby required
        if (createdBy.Trim().Length <= 0)
        {
            operationResult.Successful = false;
            operationResult.Message += "A record has not been saved in the form this entry was created by";
        }
        if (operationResult.Successful)
        {
            operationResult.DBPrimaryKey = new DAL.AccountTrackerDAL().InsertConnectionType(connectionTypeDesc.Trim(), createdBy);
            operationResult.Message = "Account Access Level Saved Successfully";
        }
        return operationResult;
    }

第二个业务逻辑方法和更新代码:

public BO.OperationResult UpdateConnectionType(int connectionTypeID, string connectionTypeDesc,bool isDisabled,string lastUpdatedBy)
        {
            var operationResult = new BO.OperationResult();

            if (connectionTypeDesc.Trim().Length <= 0)
            {
                operationResult.Successful = false;
                operationResult.Message += "Connection Type Description has not successfully updated.";
            }
            if (lastUpdatedBy.Trim().Length <= 0)
            {
                operationResult.Successful = false;
                operationResult.Message += "Last updated by must be entered.";
            }
            if (operationResult.Successful)
            {
                operationResult.DBPrimaryKey = new DAL.AccountTrackerDAL().UpdateConnectionType(connectionTypeID, lastUpdatedBy,  connectionTypeDesc,  isDisabled);
                operationResult.Message = "Account Access Level Saved Successfully";
            }
            return operationResult;        
        }

最后,我只会包含DAL层的方法签名,因为我认为这应该足够了,而不是用代码来解决这个问题。

更新ConnectionType

public int UpdateConnectionType(int connectionTypeID, string lastUpdatedBy, string connectionTypeDesc, bool isDisabled)

插入ConnectionType

 public int InsertConnectionType(string connectionTypeDesc, string createdBy)

我当前的错误读取:方法UpdateConnectionType没有重载需要6个参数。我试图默认值只是为了收到此错误。任何反馈都将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:2)

当您致电InsertConnectionType时,您必须提供四(4)个参数。这就是编写方法的方法,所以你必须这样做:

accountTrackersvc.InsertConnectionType(
  uxConnectionTypeDescTxt.Text.Trim(), 
  CommonSVC.GetUserInfoFormattedFromSession(),
  "Default", false)

上述参数将通过编译器。

如果你绝对坚持只使用两(2)个参数,你可以创建一个重载方法:

public BO.OperationResult InsertConnectionType(string connectionTypeDesc, int connectionTypeID)
{
  return InsertConnectionType(connectionTypeDesc, connectionTypeID, "Default", false);
}

更新

要为 UpdateConnectionType 方法添加重载,请尝试以下操作:

    public BO.OperationResult UpdateConnectionType(int connectionTypeID, string connectionTypeDesc)
    {
        var operationResult = new BO.OperationResult();

        if (connectionTypeDesc.Trim().Length <= 0)
        {
            operationResult.Successful = false;
            operationResult.Message += "Connection Type Description has not successfully updated.";
        }
        if (operationResult.Successful)
        {
            operationResult.DBPrimaryKey = new DAL.AccountTrackerDAL().UpdateConnectionType(connectionTypeID, "Default", connectionTypeDesc, false);
            operationResult.Message = "Account Access Level Saved Successfully";
        }
        return operationResult;
    }

当然,请确保将文本"Default"和布尔值false替换为适合您班级的内容。

相关问题