|
-
Oct 28th, 2009, 10:20 PM
#1
Thread Starter
Junior Member
Binarysearch returns -136?
So, I've got most of this figured out, I've got my text files read into an array. Which was step 1 (see my post earlier today if you're curious.)
My code so far is:
Note: textSKU is just a text box. No event handler associated.
Code:
Private Sub BtnSeacrch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSeacrch.Click
Dim objCA As New System.IO.StreamReader(ofdCA.FileName)
System.IO.StreamReader(ofdWarehouse.FileName)
'Dim StrLineCA As String
Dim SKUSheet() As String
Dim returnValueCA As String
Dim StrLineCA As String
StrLineCA = objCA.ReadToEnd
SKUSheet = StrLineCA.Split(System.Environment.NewLine)
Array.Sort(SKUSheet)
returnValueCA = Array.BinarySearch(SKUSheet, textSKU.Text)
MsgBox(SKUSheet(returnValueCA))
The message box is mostly just to test to see if it's finding the line. And the probleme is it's returning -136. Which I'm pretty sure can't be a value in the array ( actually very sure, as I tested it just to be sure, and it says it's out of bounds). I know it's in the array, as when I change returnvalueCA to a...7 for example, it outputs the correct line from the .csv file. Any ideas? Thanks in advance.
Tyler.
Edit: Index was outside the bounds of the array. is what the debugger says, as it's trying to output the value of SKUSheet(-136)
-
Oct 28th, 2009, 10:27 PM
#2
Re: Binarysearch returns -136?
What does the documentation say?
Return Value
Type: System.Int32
The index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
The documentation goes on to explain what a bitwise complement is. ALWAYS read the documentation first.
-
Oct 28th, 2009, 10:32 PM
#3
Thread Starter
Junior Member
Re: Binarysearch returns -136?
Well, that answers -why- it's doing it, and for that I thank you, but I'm not sure how to correct it? If I have it output a line from the variable I can see the SKU right there, my question is, what am I doing wrong that it has it stored in a string but it can't see it? I'm sure it's obvious, and I apologize in advance, but I'm doped up sudaphed and antibiotics ( got whatever's going around) which isn't helping the programming problem.
-
Oct 28th, 2009, 10:39 PM
#4
Re: Binarysearch returns -136?
Well, being on a medicine high is definitely not the time to program. Walk away from the program for a day or two until your medicine (Spelled Sudafed..nasty stuff btw.) and retackle it with a clear mind.
As for the problem itself, that is rather strange behavior to me, but I've never used the BinarySearch option
-
Oct 28th, 2009, 10:45 PM
#5
Thread Starter
Junior Member
Re: Binarysearch returns -136?
It is, but I have a generic brand from walgreens, they spell it Wal-phed. But good advice, I'm sick from work from whatever's going around and was/am trying to do something useful.
-
Oct 28th, 2009, 10:45 PM
#6
Re: Binarysearch returns -136?
If BinarySearch doesn't find it then it's not there. Maybe you have extra spaces in one or the other or the case is different. If that's the case the BinarySearch is overloaded and you can specify your own comparer, so you can take that sort of thing into account if needs be.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|