Question Problem with Array.IndexOf

Coffer

Member
Joined
Jun 26, 2009
Messages
7
Programming Experience
5-10
Hello!

I have a tricky little problem. I want to retrieve the index of a current occurence inside an array. I use this code:

VB.NET:
Array.IndexOf(myArray, Request.QueryString("i"))

This code ALWAYS returns 0 even if the array contains 10 indexes... I read somewhere in this thread that Array.IndexOf returns the very first occurence of a value inside the array. Does that mean that Array.IndexOf always should return 0? Since all fields in the array are unique it should return 9 if Request.QueryString("i") equals to a value in the 9th position of the array, right? Or am I thinking wrong?

If I am thinking wrong, how should I retrieve the index of a current request from the array?

// Kristofer
 
This returns 6 like expected.

VB.NET:
		Dim ints() As Integer = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

		MessageBox.Show(Array.IndexOf(ints, 7))
 
To me the problem still occurs. This is the code I am using...

VB.NET:
Dim NewStr As String = ""
Dim Directory As New DirectoryInfo(Request.QueryString("f"))
Dim File As FileInfo

For Each File In Directory.GetFiles("*.jpg")
     NewStr += File.Name.Substring(0, File.Name.Length - 4) & ","
Next

Dim myArray() As String = Split(NewStr.Substring(0, NewStr.Length - 1), ",")

' This is where the error occurs... it ALWAYS returns IndexOf = 0
Dim CurrentIndex As Integer = Array.IndexOf(myArray, Request.QueryString("i")) + 1
lblcuridx.Text = "index of " & Request.QueryString("i") & ".jpg is " & CurrentIndex

What am I doing wrong?
 
What is Request.QueryString("i") returning?

Array.IndexOf will return -1 if you don't have a match (which you're incrementing by 1)

Example using strings:

VB.NET:
		Dim strings() As String = New String() {"Document1", "Document2", "Document3", "Document4", "Document5"}

		'Returns -1
		MessageBox.Show(Array.IndexOf(strings, "Document10"))

		'Returns 2
		MessageBox.Show(Array.IndexOf(strings, "Document3"))
 
The function is used to display a navigational structure for an image gallery. Request.QueryString("i") returns the filename of the image currently displayed on the screen. With this information I can add "previous" and "next" buttons on the page.

Request.QueryString("i") returns the current Filename.
VB.NET:
lblcuridx.Text = "index of " & Request.QueryString("i") & ".jpg is " & CurrentIndex
The code above should (if everything worked as expected) show "index of IMG_0370.jpg is 12 (as the 12th index in the array holds the value IMG_0370).

I have tried to write it as a string of variables as you described. Yet the code returns "0" despite the position in the array...
 
The code works as expected, but the search is case-sensitive, could that be why?

Also, this code is better:
VB.NET:
Dim folder As String = Request.QueryString("f")
Dim search As String = Request.QueryString("i")
Dim filenames As New List(Of String)
For Each filepath As String In IO.Directory.GetFiles(folder, "*.jpg")
    filenames.Add(IO.Path.GetFileNameWithoutExtension(filepath))
Next
Dim currentIndex As Integer = filenames.IndexOf(search)
 
Finally it works. Thank you so much for the insight about case sensitivity.

VB.NET:
Array.IndexOf(myArray, UCase(strFileName)) + 1

renders the right index of the current image. As a parenthesis I wonder why the usage of this code requires me to remove "+ 1"? It works like a charm, but why does it render the wrong value when I leave "+ 1" there? It doesn't render the wrong value if I just want to present the index on the screen...
 
Array indexes are 0-based.

Also, I would prefer the String method ToUpper over the runtime function UCase, so instead of:
VB.NET:
UCase(fileName)
this:
VB.NET:
fileName.ToUpper
 
Back
Top