解决导致错误的参数的最佳方法C#

W.H*_*arr 1 .net c# debugging parameters error-handling

我尝试调用以下过程时收到错误:

ApplyVoucherNumberToBillingCharges(practiceID,
                                  Convert.ToInt32(rdoVoucherNumberAppliesTo.SelectedValue), 
                                  VoucherNumber, 
                                  VoucherNumberID,
                                  Convert.ToInt32(hdRoundingRecordId.Value),
                                  ChargeID,
                                  hdGenerateVoucherNumberChargeList.Value,
                                  Convert.ToDateTime(txtFromDate.Text),
                                  Convert.ToDateTime(txtToDate.Text),
                                  Convert.ToDateTime(txtPostingDate.Text),
                                  Convert.ToInt32(ddlVisitType.SelectedValue),
                                  SelectVisitProvider.SelectedID,
                                  SelectVisitLocation.SelectedID,
                                  chkOverwriteVoucherNumber.Checked);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

输入字符串格式不正确c#

我知道这与转换类有关,因为我之前调用不同的方法时遇到了同样的错误.区别在于只有一个参数使用Convert类,现在有几个参数.

错误信息并不像我想的那样具有描述性,也没有提供许多关于特定情况发生的线索.

除了对每个执行此操作的参数进行注释和硬编码以及查看哪个参数导致问题之外,还有更好的方法来确定问题是什么吗?

我真的很想知道,因为我的项目有时需要几分钟才能构建(我知道,这很荒谬,我已经尝试了一切来解决这个问题,但没有任何作用)所以调试这个小问题可能需要永远.

感谢您的任何帮助,您可以提供.

pix*_*ger 5

您发布的代码段是相当于连续句子的编码.你在一行代码中执行的操作太多了.这使您的代码难以阅读,并且 - 正如您所发现的 - 难以调试.

作为起点,将Convert调用结果分配给变量,然后将这些结果传递给方法:

var convertedVoucherNumberAppliesTo = Convert.ToInt32(rdoVoucherNumberAppliesTo.SelectedValue);
//other conversions here
ApplyVoucherNumberToBillingCharges(practiceId, convertedVoucherNumberAppliesTo, \\ pass in other variables here...);
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以快速识别哪个转换失败,因为每个潜在错误都将出现在特定行上.

至于您的项目需要几分钟来构建,根据项目大小,这可能是正常的.例如,我正在写这个答案,等待我们的CI服务器上的20分钟构建和测试会话完成 - 构建占用了一半的时间.