请帮助索引超出范围异常 [英] Please Help With Index Out Of Range Exception

查看:101
本文介绍了请帮助索引超出范围异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎将博耶摩尔算法转换为vb.net,但是在此代码中出现了超出范围的异常:

I have almost converted the boyer moore algorithum to vb.net but get the out of range exception in this code:

' Returns value specified by Key
' and null if the Key isn't found 
Public Function [Get](ByVal Key As Char) As Integer()
Dim HashedKey As Integer = 256
If Items(HashedKey) IsNot Nothing Then
	' The most likely variant
	If Items(HashedKey)(0).Key = Key Then
		Return Items(HashedKey)(0).Shifts
	End If
	For i As Integer = 1 To Items(HashedKey).Length - 1
		If Items(HashedKey)(i).Key = Key Then
			Return Items(HashedKey)(i).Shifts
		End If
	Next i
End If
Return Nothing



我试图使用try catch ex作为超出范围的索引的异常,并且似乎表文本框会尝试工作,但是在richtextbox1区域中找不到匹配项.


这是异常的堆栈跟踪:

在BMSearch.BMHashTable.Get(字符键)中的C:\ Users \ Admin \ Desktop \ VB Turbo Boyer Moor \ BMHashTable.vb:第26行
在BMSearch.BMHashTable.Add(Char键,Int32 []值)在C:\ Users \ Admin \ Desktop \ VB Turbo Boyer Moor \ BMHashTable.vb:line 43
在C:\ Users \ Admin \ Desktop \ VB Turbo Boyer Moor \ BMSearcher.vb:line 21中的BMSearch.BMSearcher..ctor(字符串模式)上:第21行
在BMSearch.CIBMSearcher..ctor(字符串模式,布尔型CaseSensitive)在C:\ Users \ Admin \ Desktop \ VB Turbo Boyer Moor \ CIBMSearcher.vb:line 5
在BMSearch.WinForm.button1_Click(Object sender,EventArgs e)中的C:\ Users \ Admin \ Desktop \ VB Turbo Boyer Moor \ WinForm.vb:line 138
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)中
在System.Windows.Forms.Control.WmMouseUp上(消息和m,MouseButtons按钮,Int32单击)
在System.Windows.Forms.Control.WndProc(Message& m)中
在System.Windows.Forms.ButtonBase.WndProc(Message& m)中
在System.Windows.Forms.Button.WndProc(Message& m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)中
在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,Int32 pvLoopData)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)中
在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在BMSearch.WinForm.Main()中的C:\ Users \ Admin \ Desktop \ VB Turbo Boyer Moor \ WinForm.vb:line 134
在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)中
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态,布尔值ignoreSyncCtx)处
在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态)处
在System.Threading.ThreadHelper.ThreadStart()处


预先感谢您的帮助:)



I have tried to use the try catch ex as index out of range exception and it seems that the table textbox tries to then work but no match is found in the richtextbox1 area.


This is the stack trace of the exception:

at BMSearch.BMHashTable.Get(Char Key) in C:\Users\Admin\Desktop\VB Turbo Boyer Moor\BMHashTable.vb:line 26
at BMSearch.BMHashTable.Add(Char Key, Int32[] Value) in C:\Users\Admin\Desktop\VB Turbo Boyer Moor\BMHashTable.vb:line 43
at BMSearch.BMSearcher..ctor(String Pattern) in C:\Users\Admin\Desktop\VB Turbo Boyer Moor\BMSearcher.vb:line 21
at BMSearch.CIBMSearcher..ctor(String Pattern, Boolean CaseSensitive) in C:\Users\Admin\Desktop\VB Turbo Boyer Moor\CIBMSearcher.vb:line 5
at BMSearch.WinForm.button1_Click(Object sender, EventArgs e) in C:\Users\Admin\Desktop\VB Turbo Boyer Moor\WinForm.vb:line 138
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at BMSearch.WinForm.Main() in C:\Users\Admin\Desktop\VB Turbo Boyer Moor\WinForm.vb:line 134
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Thank you in advance for the help :)

推荐答案

那么,什么是Items?锯齿状数组或其他东西?

好吧,从抛出异常的行来看,听起来Items(256)超出范围,大概是因为数组不那么大.

创建一个设置为256的HashedKey并没有任何意义,然后检查Items(256)是否不为null. 256是一个常量,这是运行时检查.
So, what is Items? A jagged array or some other thing?

Well, judging by the line throwing the exception it sounds like Items(256) is out of range, presumably because the array isn''t that big.

Doesn''t make much sense to create a HashedKey set to 256, then checking to see if Items(256) is not null. 256 is a constant, and that''s a runtime check.


这篇关于请帮助索引超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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