473,902 Members | 5,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array.IndexOf - not working

Al

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

But IndexOf on a specific item in that array returns a value of 26:
Find1 = FilesArray(2).I ndexOf("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 12872
Al,

Are you sure of that, this gives for me 1
\\\
Dim filesarray() As String = {"sa000", "sa001", "sa002"}
Dim Find1 As Integer = Array.IndexOf(f ilesarray, "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**********@h otmail.com> wrote:

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

But IndexOf on a specific item in that array returns a value of 26:
Find1 = FilesArray(2).I ndexOf("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.ChangeMvpCa tegory("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.GetFi les(sFolder)
All items are unique, since they are file names with path (e.g.
"C:\imageProjec t\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**********@h otmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
:
: This statement returns a -1, indicating "not found":
: Find1 = Array.IndexOf(F ilesArray, "sa001")
:
: But IndexOf on a specific item in that array returns a value of 26:
: Find1 = FilesArray(2).I ndexOf("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.writeli ne(Array.IndexO f(s1, "sa001"))
console.writeli ne(Array.IndexO f(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.writeli ne(Array.IndexO f(s1, "sa001"))

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

if ndx <= s1.getupperboun d(0) then
console.writeli ne(ndx)
else
console.writeli ne("-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.GetFi les(FolderName)
Each item is a file name with path (e.g."C:\ImageP roject\sa001.ti f")

2. Create a mirror array FilesArray2 (additional step, but one-time).
Using string.LastInde xOf 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**********@h otmail.com> escribió en el mensaje
news:11******** **************@ g47g2000cwa.goo glegroups.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.GetFi les(FolderName)
Each item is a file name with path (e.g."C:\ImageP roject\sa001.ti f")

2. Create a mirror array FilesArray2 (additional step, but one-time).
Using string.LastInde xOf 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**********@h otmail.com> escribió en el mensaje
news:11******** **************@ g49g2000cwa.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2435
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 Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat") var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec
2
12984
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 Any suggestion?
8
3048
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 5,000 elements, and I need to find the string, say it's "Start here". I've tried this: Dim MyObj as Object = "Start Here" Dim I as Integer = System.Array.BinarySearch(MyArr,MyObj)
2
4654
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 that will do this easily? I've tried messing with Array.IndexOf but can't seem to get it to indicate where the byte sequnce starts.
1
1510
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 replace String* with Int32, the code would not compile? The code using String* type: #include "stdafx.h"
5
1670
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 <stdlib.h> #define dim 2
3
1612
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 expected. If I fetch the values from the web.config this way: clmns() As String =
1
1259
by: eknee1417 | last post by:
' Determine what roles the selected user belongs to Dim strSelectedUserName As String = ddlUserList.SelectedValue Dim strSelectedUsersRoles() As String = Roles.GetRolesForUser(strSelectedUserName) Dim strRoleNames() As String = Roles.GetAllRoles.Clone ' Loop through the Repeater's Items and check or uncheck the checkbox as needed For Each ri As RepeaterItem In...
2
2836
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 an array: // find max and min value foreach( $value as $__vals )
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11279
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.