This returns 6 like expected.
Code:Dim ints() As Integer = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} MessageBox.Show(Array.IndexOf(ints, 7))
|
|
Hello!
I have a tricky little problem. I want to retrieve the index of a current occurence inside an array. I use this code:
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?Code:Array.IndexOf(myArray, Request.QueryString("i"))
If I am thinking wrong, how should I retrieve the index of a current request from the array?
// Kristofer
This returns 6 like expected.
Code: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...
What am I doing wrong?Code: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 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:
Code: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.
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).Code:lblcuridx.Text = "index of " & Request.QueryString("i") & ".jpg is " & CurrentIndex
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:
Code: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)
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly
Finally it works. Thank you so much for the insight about case sensitivity.
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...Code:Array.IndexOf(myArray, UCase(strFileName)) + 1
Array indexes are 0-based.
Also, I would prefer the String method ToUpper over the runtime function UCase, so instead of:
this:Code:UCase(fileName)
Code:fileName.ToUpper
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly
Thanks for the improvement.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks