索引超出范围错误,当它不是时

时间:2013-03-11 11:05:04

标签: c# arrays

我有以下方法:

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;
        }
    }
}

很简单,但是当索引看起来不在范围之外时,我得到间歇性的“索引超出范围”错误:

Out of range

这是StackTrace:

  

at System.ThrowHelper.ThrowArgumentOutOfRangeException()at   System.Collections.Generic.List 1.get_Item(Int32 index) at System.Collections.ObjectModel.Collection 1.get_Item(Int32 index)
  在   Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfWagerIsValid(CouponViewModel   结果,Bet bet,String wagerType,Int32 selectionCount)in   C:\代码\ Arkle \ Arkle \ Arkle.CouponProcessing \扫描\ LonglistDecoder_994550.cs:线   117点   Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfBetIsValid(CouponViewModel   造成   C:\代码\ Arkle \ Arkle \ Arkle.CouponProcessing \扫描\ LonglistDecoder_994550.cs:线   107 at Arkle.CouponProcessing.Scan.LonglistDecoder_994550.Decode()   在   C:\代码\ Arkle \ Arkle \ Arkle.CouponProcessing \扫描\ LonglistDecoder_994550.cs:线   62在ArkleWPF.UI.SlipScanning.CouponTools.DecodeCoupon(Image img,   OMRForm omrForm1,CouponDecoder解码器,CouponPrintingInformation   viewSettings,String slipBarcode,DecodeStatus status)in   C:\ code \ Arkle \ Arkle \ ArkleWPF \ UI \ SlipScanning \ CouponTools.vb:第215行   在ArkleWPF.UI.SlipScanning.CouponTools.ProcessForm(OMRForm omrForm1,   DecodeStatus status,CouponPrintingInformation viewSettings,Boolean   alwaysLotto)in   C:\ code \ Arkle \ Arkle \ ArkleWPF \ UI \ SlipScanning \ CouponTools.vb:第89行   在ArkleWPF.UI.SlipScanning.CouponTools._Closure $ __ 1._Lambda $ __ 1()in   C:\ code \ Arkle \ Arkle \ ArkleWPF \ UI \ SlipScanning \ CouponTools.vb:第27行
  在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
  在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态)at   System.Threading.ThreadHelper.ThreadStart()

每次都不会发生这种情况,更像是每隔一两次,这让我发疯了!有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

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