由于"System.IndexOutOfRangeException"而导致的数组头痛 [英] Array Headaches due to 'System.IndexOutOfRangeException'

查看:126
本文介绍了由于"System.IndexOutOfRangeException"而导致的数组头痛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个用于d20样式字符创建的压模程序...主要是看是否可以,但是我不断遇到以下错误:

WindowsApplication1.exe中出现类型为"System.IndexOutOfRangeException"的第一次机会异常

以下是与所讨论的数组相关的直接关联的代码块:

I am trying to write a die rolling program for d20 style character creation... Mostly just to see if I can, but I keep getting the following error:

A first chance exception of type ''System.IndexOutOfRangeException'' occurred in WindowsApplication1.exe

Here are the immediately associated blocks of code related to the array in question:

Dim strTemp(5) As Integer, sti As Integer 'initialize the array







and

For sti = 0 To dieNum
    temproll = GetNext()
    If reNum > 0 And temproll <= reNum Then temproll = Reroll()
    strTemp(sti) = temproll
    strRoll = strRoll + temproll
Next



令人费解的是,有6个这样的块重复此过程,并且显然涉及2个单独的函数,但是代码中特别突出显示的部分是strTemp(sti) = temproll行.当sti出错时sti的值为6,但是dieNum的最大可能数为5.即使当它为1或2时,我仍然遇到相同的错误.谁能告诉我我在哪里搞砸?顺便说一句,如果需要更多代码,我会很乐意.

经过一番审查后,问题显然在于未设置dieNum.之前未提及的代码dieNum = Asc(cboNum.Text)似乎是罪魁祸首.关于如何将组合框的数字内容转换为整数的任何想法?我以为我有这个权利.



It is wort noting that there are 6 such blocks repeating this process, and obviously 2 seperate functions involved, but the part of the code that specifically gets highlighted is the strTemp(sti) = temproll line. The value of sti is 6 when it errors, but the max possible number for dieNum is 5. Even when it is 1 or 2 I still get the same error. Can anyone tell me where I am screwing up? BTW if more code is needed, I will gladly oblige.

after some review apparently the problem is that dieNum is not being set. The not before mentioned code dieNum = Asc(cboNum.Text) seems to be the culprit. Any ideas on how to cast the numeric contents of a combobox to an integer? I thought I had this right.

推荐答案

如果dieNum为5,则循环肯定会抛出IndexOutOfRangeException:在这种情况下,范围是0到dieNum产生6个值,但strTemp仅包含5个元素.

对于傻笑,请参见 http://en.wikipedia.org/wiki/Off-by-one_error [ ^ ] :-)

—SA
If dieNum is 5, the loop certainly will throw IndexOutOfRangeException: in this case the range 0 to dieNum yields 6 values, but strTemp contains only 5 elements.

For giggles, see http://en.wikipedia.org/wiki/Off-by-one_error[^] :-)

—SA


好吧,看来这是一个ID10T错误...对于其他可能有这种特殊屁的人,解决方案很简单. dieNum = Asc(cboNum.Text)应该是dieNum = Convert.ToInt32(cboNum.Text).使用转换时出现简单错误……干杯!
Okay, so it appears as if this was an ID10T error... For anyone else that may have this particular brain fart, the solution was simple. dieNum = Asc(cboNum.Text) should have been dieNum = Convert.ToInt32(cboNum.Text). Simple error in using conversions... Cheers!


这篇关于由于"System.IndexOutOfRangeException"而导致的数组头痛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆