473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Array.IndexOf - not working

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
11 12794
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
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
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

"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
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
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
"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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Treetop | last post by:
I cannot get this array to work. I want to have the game listed until the day after the event, then come off the list. function events() { var today = new Date(); var dayarray=new...
2
by: Shang Wenbin | last post by:
Hi, I use the following snip: myarray = new Array(2, 5, 9); var index = myarray.indexOf(2); but it is not workable, the debugger says array.indexOf is not a function.I use mozilla 1.7.10 ...
8
by: Sueffel | last post by:
Okay, I can certinatly use a For..Next loop to find the specified info I need, but I'm thinking the BinarySearch will be faster, if I can get it working LOL Anyhew, got an array, may have 5 or...
2
by: Larry | last post by:
I have a Byte Array Dim A1() as byte = {1,2,3,4,9,9,9,11,12,13,14,9,9,9} I want to find the location of the first occurance of the byte sequence {9,9,9}. Is there a built in Framework class...
1
by: Allen Maki | last post by:
Why can I get the index of the item of the array when I use string*, but can not get the index of the array when I use any other type (such as Int32)? This code will compile perfectly, but if I...
5
by: =?ISO-8859-1?Q?Jonathan_Gro=DF?= | last post by:
Hi everybody, in the sample below my programm gives a "Bus error" on the marked line. I don't get it why that happens. I followed the instructions in the C-FAQ. #include <math.h> #include...
3
by: Morten71 | last post by:
I have a strange problem. I have a local string() var I populate this way: clmns() As String = {"InvoiceNo", "InvoiceDate"} When I call: Array.IndexOf(clmns,"InvoiceDate") I get 0 (zero) as...
1
by: eknee1417 | last post by:
' Determine what roles the selected user belongs to Dim strSelectedUserName As String = ddlUserList.SelectedValue Dim strSelectedUsersRoles() As String =...
2
by: luftikus143 | last post by:
Hi there, I have an array, which stores in the first dimension a counter for the number of sets, and in the second the value: $value I found somewhere how to find min and max values in such...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.