Array.BinarySearch ALWAYS NEGETIVE

  • Thread starter Thread starter Guest
  • Start date Start date
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();
}
 
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.
 
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.
 
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"
 
Back
Top