Array.BinarySearch ALWAYS NEGETIVE

G

Guest

Why doesn’t this work? If State="NY", the resulting IF statement returns 31,
if the State="NC" the resulting IF returns -27, yet NC is in the array list.
Its random as to which stats work and which do not. AL will return 0, but AR
will return -3, yet its in the list. I know the value when neg (-) is a
bitwise representation, but most every state in the array list returns a neg
number, Why?

string[] strStateArray = new string[]
{"AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IN","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY"};


if(Array.BinarySearch(strStateArray, State.ToString())<0)
{
ValidationStatus="Incoming State is invalid. The follow state
abrevations are acceptable: " + strStateArray.ToString();
}
 
P

pvdg42

JP said:
Why doesn't this work? If State="NY", the resulting IF statement returns
31,
if the State="NC" the resulting IF returns -27, yet NC is in the array
list.
Its random as to which stats work and which do not. AL will return 0, but
AR
will return -3, yet its in the list. I know the value when neg (-) is a
bitwise representation, but most every state in the array list returns a
neg
number, Why?

string[] strStateArray = new string[]
{"AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IN","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY"};


if(Array.BinarySearch(strStateArray, State.ToString())<0)
{
ValidationStatus="Incoming State is invalid. The follow state
abrevations are acceptable: " + strStateArray.ToString();
}

The elements to be searched by a binary search algorithm must be sorted.
Your failures (AZ, NC, etc) are out of order, and therefore, not found.
 
G

Guest

Oh duh! Thank you so much. Its been one of those Fridays.
--
JP
..NET Software Develper


pvdg42 said:
JP said:
Why doesn't this work? If State="NY", the resulting IF statement returns
31,
if the State="NC" the resulting IF returns -27, yet NC is in the array
list.
Its random as to which stats work and which do not. AL will return 0, but
AR
will return -3, yet its in the list. I know the value when neg (-) is a
bitwise representation, but most every state in the array list returns a
neg
number, Why?

string[] strStateArray = new string[]
{"AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IN","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY"};


if(Array.BinarySearch(strStateArray, State.ToString())<0)
{
ValidationStatus="Incoming State is invalid. The follow state
abrevations are acceptable: " + strStateArray.ToString();
}

The elements to be searched by a binary search algorithm must be sorted.
Your failures (AZ, NC, etc) are out of order, and therefore, not found.
 
B

Ben Voigt

pvdg42 said:
JP said:
Why doesn't this work? If State="NY", the resulting IF statement returns
31,
if the State="NC" the resulting IF returns -27, yet NC is in the array
list.
Its random as to which stats work and which do not. AL will return 0, but
AR
will return -3, yet its in the list. I know the value when neg (-) is a
bitwise representation, but most every state in the array list returns a
neg
number, Why?

string[] strStateArray = new string[]
{"AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IN","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY"};


if(Array.BinarySearch(strStateArray, State.ToString())<0)
{
ValidationStatus="Incoming State is invalid. The follow state
abrevations are acceptable: " + strStateArray.ToString();
}

The elements to be searched by a binary search algorithm must be sorted.
Your failures (AZ, NC, etc) are out of order, and therefore, not found.

More precisely, it looks like the state names, not the abbreviations, were
sorted
"New York" < "North Carolina" but "NC" < "NY"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top