By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

Array.IndexOf - not working

P: n/a
Al

This statement returns a -1, indicating "not found":
Find1 = Array.IndexOf(FilesArray, "sa001")

But IndexOf on a specific item in that array returns a value of 26:
Find1 = FilesArray(2).IndexOf("sa001")
This shows the value "sa001" is found in FilesArray.

Any suggestions why Array.IndexOf does not return a number (index of
item that has the value)?

Thanks,
Al

Nov 21 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Al,

Are you sure of that, this gives for me 1
\\\
Dim filesarray() As String = {"sa000", "sa001", "sa002"}
Dim Find1 As Integer = Array.IndexOf(filesarray, "sa001")
///

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a
Dear Al,

Just a confirmation. Is the array "FilesArray" multi dimension?
It seems so.

The document said "Returns the index of the first occurrence
of a value in a *one-dimensional* Array or in a portion of
the Array. "

On 1 Jun 2005 19:42:46 -0700
"Al" <al**********@hotmail.com> wrote:

This statement returns a -1, indicating "not found":
Find1 = Array.IndexOf(FilesArray, "sa001")

But IndexOf on a specific item in that array returns a value of 26:
Find1 = FilesArray(2).IndexOf("sa001")
This shows the value "sa001" is found in FilesArray.

Any suggestions why Array.IndexOf does not return a number (index of
item that has the value)?

Thanks,
Al

---
MVP kaok = MVP.ChangeMvpCategory("for C# 2004-2005.");
kaok.Web = "http://www.antoine.st/";

Nov 21 '05 #3

P: n/a
Al
Thanks for your replies.

The array is one dimension:
Public FilesArray() As String

The application needs to search for words in file names.

I do this:
FilesArray = Directory.GetFiles(sFolder)
All items are unique, since they are file names with path (e.g.
"C:\imageProject\sa002.tif")

I then use a For-Next loop looking at each item in the array with
IndexOf:
Find1 = FilesArray(Fi).IndexOf(word)
This works OK but with larger array, I thought Array.IndexOf would be
faster.

Cor, the simple example you provided is looking at the word at the
beginning of the string. I had wondered if that was needed for
Array.IndexOf. But I did a test, searching for "C:\", but it still
returned with -1.

It just appears that Array.IndexOf is not working for some reason on
FilesArray. I'll test with the simple example; perhaps it will turn a
light on.

I have Windows XP home edition.

Considering what the app needs, are there better ways to do this?

Al

Nov 21 '05 #4

P: n/a

"Al" <al**********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
:
: This statement returns a -1, indicating "not found":
: Find1 = Array.IndexOf(FilesArray, "sa001")
:
: But IndexOf on a specific item in that array returns a value of 26:
: Find1 = FilesArray(2).IndexOf("sa001")
: This shows the value "sa001" is found in FilesArray.
:
: Any suggestions why Array.IndexOf does not return a number (index of
: item that has the value)?
:
: Thanks,
: Al
I compiled the following test. Note that s2 contains elements that
include "sa00#" as part of the value in each element, not the full
value:

================================================== =
imports system
public class [class]

public shared sub main
dim s1 as string() = {"sa001", "sa002", "sa003"}
dim s2 as string() = {"asa001a", "bsa002b", "csa003c"}

console.writeline(Array.IndexOf(s1, "sa001"))
console.writeline(Array.IndexOf(s2, "sa001"))

end sub
end class
=================================================
This generated the following response:

=================================================
0
-1
=================================================
The 1st writeline statement displayed 0 because there is a string in
array s1 that matches the exact value "sa001". However, the 2nd line
displayed -1 because no element in array s2 exactly matches that test
string. The Array.IndexOf() function will only find the test string if
the entire string equals that value, not just part of the string.
Try something along these lines instead

=================================================
imports system
public class [class]

public shared sub main
dim s1 as string() = {"sa001", "sa002", "sa003"}
dim s2 as string() = {"asa001a", "bsa002b", "csa003c"}

console.writeline(Array.IndexOf(s1, "sa001"))

'*******************************************
dim ndx As Integer
for ndx = 0 to s1.getUpperBound(0)
If s1(ndx).indexOf("sa001") > -1 Then
exit for
End If
next

if ndx <= s1.getupperbound(0) then
console.writeline(ndx)
else
console.writeline("-1")
end if
'*******************************************

end sub
end class
=================================================
(Note: this will only tell you which element in array s2 contains the
string "sa001", not where it is in that specific string)
HTH

Ralf
Nov 21 '05 #5

P: n/a
Al
Thanks Ralf,

I was expecting Array.IndexOf to work the same way as String.IndexOf.

The .NET Help provides this information:

String.IndexOf Method:
Reports the index of the first occurrence of a String, or one or more
characters, within this instance.

Array.IndexOf Method:
Returns the index of the first occurrence of a value in a
one-dimensional Array or in a portion of the Array.

These sound very similar.
IMO there should be an inexpensive note in Array.IndexOf Method:
"The search value must match the whole array item, and is therefore
different than String.IndexOf."

Al

Nov 21 '05 #6

P: n/a
Al
This is how I changed the app, utilizing Array.IndexOf.

One time steps:
1. Create the files array (as was done before).
FilesArray = Directory.GetFiles(FolderName)
Each item is a file name with path (e.g."C:\ImageProject\sa001.tif")

2. Create a mirror array FilesArray2 (additional step, but one-time).
Using string.LastIndexOf in a For/Next loop, extract the file name
(e.g. "sa001") from each FilesArray item. Both arrays have same index
numbers.

When user selects an item (e.g. clicks "Next" button):
3a. Data file (which may be sorted different ways) provides the file
name ("e.g. "sa001").
3b. Use Array.IndexOf on FilesArray2 to find that item.
3c. Use that index on FilesArray to get the file path.
3d. Display image file.

When array is 1000 or more, this should provide faster and more
consistent performance than a For/Next loop. User may click "Next"
button fast.

Al

Nov 21 '05 #7

P: n/a
"Al" <al**********@hotmail.com> escribió en el mensaje
news:11**********************@g47g2000cwa.googlegr oups.com...
This is how I changed the app, utilizing Array.IndexOf.

One time steps:
1. Create the files array (as was done before).
FilesArray = Directory.GetFiles(FolderName)
Each item is a file name with path (e.g."C:\ImageProject\sa001.tif")

2. Create a mirror array FilesArray2 (additional step, but one-time).
Using string.LastIndexOf in a For/Next loop, extract the file name
(e.g. "sa001") from each FilesArray item. Both arrays have same index
numbers.

When user selects an item (e.g. clicks "Next" button):
3a. Data file (which may be sorted different ways) provides the file
name ("e.g. "sa001").
3b. Use Array.IndexOf on FilesArray2 to find that item.
3c. Use that index on FilesArray to get the file path.
3d. Display image file.

When array is 1000 or more, this should provide faster and more
consistent performance than a For/Next loop. User may click "Next"
button fast.
Array.IndexOf is doing the For/Next loop for you, so this paragraph does not
apply.
Al


Best Regards
Alejandro Lapeyre
Nov 21 '05 #8

P: n/a
Al

Array.IndexOf does something for the programmer, making it much easier
than a For/Next loop.

The important thing I learned here is the array item must match the
whole search value, unlike String.IndexOf.
In this app, if the array is large enough, I think the performance is
better when the user is clicking from item to item, and it takes just a
few seconds to create the second array up front.

Please explain what paragraph does not apply.
Al

Nov 21 '05 #9

P: n/a

The paragraph where you said that using Array.IndexOf is faster than using a
For/Next loop

Array.IndexOf uses a For/Next loop to find the item.

Best Regards
Alejandro Lapeyre

"Al" <al**********@hotmail.com> escribió en el mensaje
news:11**********************@g49g2000cwa.googlegr oups.com...

Array.IndexOf does something for the programmer, making it much easier
than a For/Next loop.

The important thing I learned here is the array item must match the
whole search value, unlike String.IndexOf.
In this app, if the array is large enough, I think the performance is
better when the user is clicking from item to item, and it takes just a
few seconds to create the second array up front.

Please explain what paragraph does not apply.
Al

Nov 21 '05 #10

P: n/a
Al
Sorry for the delay. I did some performance testing, learning about
timespan and formatting the results.
If the array is very large, the "two array" method I described is
faster.

But the difference is subsecond even if the array has one million
items:
Two array method, selecting "1000000" (one million) did it in 46 to 109
milliseconds.
One array method did it in 531 to 875 milliseconds.

Selecting "10000" (ten thousand) was "zero" milliseconds in both
methods.
In my app, the array size is expected to be smaller than 10,000. So
there is no advantage for this app to use Array.IndexOf.

I discovered some interesting things about timespan - at least on my
computer.
The system reports zero if the number of ticks is less than 156250 (15
milliseconds).
To test the result formatting, I needed to artificially increase the
timespan above 15 milliseconds.
I assume there is a good reason not to report the ACTUAL number of
ticks.

The system reports timespan in increments of 156250 ticks.

Al

Nov 21 '05 #11

P: n/a
Hello Al,

Don't you think it would be easier to use the Visual Basic function Filter?:
http://msdn.microsoft.com/library/de...asp?frame=true

Regards.
"Al" <al**********@hotmail.com> escribió en el mensaje news:11**********************@g47g2000cwa.googlegr oups.com...
| This is how I changed the app, utilizing Array.IndexOf.
|
| One time steps:
| 1. Create the files array (as was done before).
| FilesArray = Directory.GetFiles(FolderName)
| Each item is a file name with path (e.g."C:\ImageProject\sa001.tif")
|
| 2. Create a mirror array FilesArray2 (additional step, but one-time).
| Using string.LastIndexOf in a For/Next loop, extract the file name
| (e.g. "sa001") from each FilesArray item. Both arrays have same index
| numbers.
|
| When user selects an item (e.g. clicks "Next" button):
| 3a. Data file (which may be sorted different ways) provides the file
| name ("e.g. "sa001").
| 3b. Use Array.IndexOf on FilesArray2 to find that item.
| 3c. Use that index on FilesArray to get the file path.
| 3d. Display image file.
|
| When array is 1000 or more, this should provide faster and more
| consistent performance than a For/Next loop. User may click "Next"
| button fast.
|
| Al
|
Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.