c# - 索引超出范围错误,如果不是

标签 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.List1.get_Item(Int32 index) at System.Collections.ObjectModel.Collection1.get_Item(Int32 index)
at Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfWagerIsValid(CouponViewModel result, Bet bet, String wagerType, Int32 selectionCount) in c:\code\Arkle\Arkle\Arkle.CouponProcessing\Scan\LonglistDecoder_994550.cs:line 117 at Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfBetIsValid(CouponViewModel result) in c:\code\Arkle\Arkle\Arkle.CouponProcessing\Scan\LonglistDecoder_994550.cs:line 107 at Arkle.CouponProcessing.Scan.LonglistDecoder_994550.Decode() in c:\code\Arkle\Arkle\Arkle.CouponProcessing\Scan\LonglistDecoder_994550.cs:line 62 at ArkleWPF.UI.SlipScanning.CouponTools.DecodeCoupon(Image img, OMRForm omrForm1, CouponDecoder decoder, CouponPrintingInformation viewSettings, String slipBarcode, DecodeStatus status) in C:\code\Arkle\Arkle\ArkleWPF\UI\SlipScanning\CouponTools.vb:line 215
at ArkleWPF.UI.SlipScanning.CouponTools.ProcessForm(OMRForm omrForm1, DecodeStatus status, CouponPrintingInformation viewSettings, Boolean alwaysLotto) in C:\code\Arkle\Arkle\ArkleWPF\UI\SlipScanning\CouponTools.vb:line 89
at ArkleWPF.UI.SlipScanning.CouponTools._Closure$__1._Lambda$__1() in C:\code\Arkle\Arkle\ArkleWPF\UI\SlipScanning\CouponTools.vb:line 27
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at 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 语句中引发的。

关于c# - 索引超出范围错误,如果不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15336998/

相关文章:

c# - 测试 url 参数的值,然后从 c# 后面的代码更改 SqlDataSource.SelectCommand

c# - C# Windows Phone XNA 应用程序中的可选参数

php - 将mysql数据显示为数组

c# - int.TryParse 输出到一个对象数组

java - 基于整数定界符在java中拆分字符串

C# App.xaml 和资源字典

c# - 引入静态方法

php - 过滤掉不需要的元素后将一维数组转换为二维

javascript - window.location 未正确更新

javascript - Array Reduce Polyfill 说明