我有以下方法:

private void DetermineIfWagerIsValid(CouponViewModel result, Bet bet, string wagerType, int selectionCount)
{
    if (bet.Wagers[0].WagerType == wagerType) //error here
    {
        if (bet.Selections.Count != selectionCount)
        {
            bet.BetStatus = BetStatus.FilledInAndInvalid;
        }
    }
}


很简单,但是当索引似乎没有超出范围时,我会收到间歇性的“索引超出范围”错误:



这是StackTrace:


  在System.ThrowHelper.ThrowArgumentOutOfRangeException()处
  System.Collections.Generic.List 1.get_Item(Int32 index) at System.Collections.ObjectModel.Collection 1.get_Item(Int32索引)
  在
  Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfWagerIsValid(CouponViewModel
  结果,下注,字符串下注类型,Int32 selectionCount)
  c:\ code \ Arkle \ Arkle \ Arkle.CouponProcessing \ Scan \ LonglistDecoder_994550.cs:line
  117点
  Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfBetIsValid(CouponViewModel
  导致
  c:\ code \ Arkle \ Arkle \ Arkle.CouponProcessing \ Scan \ LonglistDecoder_994550.cs:line
  107在Arkle.CouponProcessing.Scan.LonglistDecoder_994550.Decode()
  在
  c:\ code \ Arkle \ Arkle \ Arkle.CouponProcessing \ Scan \ LonglistDecoder_994550.cs:line
  62 at ArkleWPF.UI.SlipScanning.CouponTools.DecodeCoupon(Image img,
  OMRForm omrForm1,CouponDecoder解码器,CouponPrintingInformation
  viewSettings,字符串slipBarcode,DecodeStatus状态)
  C:\ code \ Arkle \ Arkle \ ArkleWPF \ UI \ SlipScanning \ CouponTools.vb:第215行
  在ArkleWPF.UI.SlipScanning.CouponTools.ProcessForm(OMRForm omrForm1,
  DecodeStatus状态,CouponPrintingInformation viewSettings,布尔值
  alwaysLotto)中
  C:\ code \ Arkle \ Arkle \ ArkleWPF \ UI \ SlipScanning \ CouponTools.vb:第89行
  在ArkleWPF.UI.SlipScanning.CouponTools._Closure $ __ 1._Lambda $ __ 1()中
  C:\ code \ Arkle \ Arkle \ ArkleWPF \ UI \ SlipScanning \ CouponTools.vb:第27行
  在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
  在System.Threading.ExecutionContext.RunInternal(ExecutionContext
  executionContext,ContextCallback回调,对象状态,布尔值
  reserveSyncCtx)位于
  System.Threading.ExecutionContext.Run(ExecutionContext
  executionContext,ContextCallback回调,对象状态,布尔值
  reserveSyncCtx)位于
  System.Threading.ExecutionContext.Run(ExecutionContext
  executeContext,ContextCallback回调,对象状态),位于
  System.Threading.ThreadHelper.ThreadStart()


并非每次都发生,更像是第二次或第三次,这让我发疯!有任何想法吗?

最佳答案

 System.Collections.Generic.List1.get_Item(Int32 index) at
 System.Collections.ObjectModel.Collection1.get_Item(Int32 index)


请求的索引在列表查找中不存在。 Wagers是一个数组,但是WagerType没有所请求的索引。正在从列表的exception语句中引发get

10-08 13:17