方法重载和参数6错误

本文关键字:错误 参数 重载 方法 | 更新日期: 2023-09-27 18:00:43

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

背景:作为我项目的一部分,我有不同的层接口。

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

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

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)

插入连接类型

 public int InsertConnectionType(string connectionTypeDesc, string createdBy)

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

方法重载和参数6错误

调用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替换为适合您的类的内容。