【问题标题】:Method Overload and argument 6 error方法重载和参数 6 错误
【发布时间】:2015-07-01 14:42:44
【问题描述】:

我已经修改了我的代码和问题,以更好地反映我想要完成的任务。

背景:我的项目中有不同的层接口。

  • 服务层 - 处理我的业务逻辑,验证条目,(大脑)
  • 数据访问层 - 只执行它传递的方法或函数
  • 需要执行方法的 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();

再次处理我的业务逻辑的服务层格式如下。请注意,使用了 2 种不同的方法,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 层的方法签名,因为我认为这应该足够了,并且不会用代码使这个问题饱和。

更新连接类型

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

插入连接类型

 public int InsertConnectionType(string connectionTypeDesc, string createdBy)

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

【问题讨论】:

  • InsertConnectionType 的方法定义是什么? [方法签名右键->在Visual Studio中查看方法定义]
  • 除非您使用默认参数,否则无法使用 2 调用需要 4 个参数的方法。我不清楚你希望诚实
  • @now 他不能被命名我右键单击然后选择转到定义(我相信这是相同的),但它随后将我带到了持有我发布的第一个代码块的班级在我的问题中。
  • 你想做什么?您尚未发布完整的方法调用,但根据您的异常,您只传递了 2 个参数,而函数需要 4 个参数。
  • @user4966755:如果是这样,那么您必须提供 4 个参数

标签: c# .net c#-4.0


【解决方案1】:

当您调用 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 替换为适合您班级的任何内容。

【讨论】:

  • 我已经让 InsertConnectionType 方法工作了,你对我如何让 UpdateConnectionType 工作有什么建议吗?我尝试添加列出的默认值,但没有成功。
  • @user4966755 - 我添加了一个 Update 部分,应该大致处理您的请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多