Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have almost converted the boyer moore algorithum to vb.net but get the out of range exception in this code:

VB
' 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


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 :)
Posted
Updated 16-Nov-11 2:47am
v2
Comments
Richard MacCutchan 16-Nov-11 8:49am    
1. Please use the correct tags (<pre>) around your code.
2. Give us a clue as to where line 26 is, and the value of the offending index.
Dale 2012 16-Nov-11 9:05am    
Line 26 is in this function:


(<pre>)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
End Function(<pre>)


Line 26 reads : If Items(HashedKey) IsNot Nothing Then
Dale 2012 16-Nov-11 9:13am    
Also the exception expression is:

Index was outside the bounds of the array.
Dale 2012 16-Nov-11 9:15am    
I am unsure of the index? :( where can i find that information in the stack trace?

1 solution

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.
 
Share this answer
 
Comments
Dale 2012 16-Nov-11 10:35am    
what do you propose I fix? or change then?


the items variable you are asking about refers to:

private items ()() as BMSearch.BMHashTable.BMHashItem
Dale 2012 16-Nov-11 10:38am    
The project I am converting to vb.net from C# is located @

http://www.codeproject.com/KB/recipes/BoyerMooreSearch.aspx?display=PrintAll
Dale 2012 16-Nov-11 11:17am    
Thank you very much for trying to help but I have fixed the error

this question is closed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900