![]() |
Click here to advertise with us
|
|
|||||||
| ASP.NET General Discussion General discussion on ASP.NET development with VB.NET |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Hello!
I have a tricky little problem. I want to retrieve the index of a current occurence inside an array. I use this code: 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 |
|
|||
|
To me the problem still occurs. This is the code I am using...
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. 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.
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: 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 ![]() |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|