+ Reply to Thread
Results 1 to 9 of 9

Thread: Problem with Array.IndexOf

  1. #1
    Coffer is offline VB.NET Forum Newbie Coffer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2009
    Age
    30
    Posts
    7
    Reputation
    0

    Default Problem with Array.IndexOf

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

  2. #2
    MattP is offline VB.NET Forum All-Mighty MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    USA
    Posts
    1,039
    Reputation
    534

    Default

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

  3. #3
    Coffer is offline VB.NET Forum Newbie Coffer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2009
    Age
    30
    Posts
    7
    Reputation
    0

    Default

    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 am I doing wrong?

  4. #4
    MattP is offline VB.NET Forum All-Mighty MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    USA
    Posts
    1,039
    Reputation
    534

    Default

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

  5. #5
    Coffer is offline VB.NET Forum Newbie Coffer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2009
    Age
    30
    Posts
    7
    Reputation
    0

    Default

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

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    11,043
    Reputation
    1563

    Default

    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)

  7. #7
    Coffer is offline VB.NET Forum Newbie Coffer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2009
    Age
    30
    Posts
    7
    Reputation
    0

    Default

    Finally it works. Thank you so much for the insight about case sensitivity.

    Code:
    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...

  8. #8
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    11,043
    Reputation
    1563

    Default

    Array indexes are 0-based.

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

  9. #9
    Coffer is offline VB.NET Forum Newbie Coffer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2009
    Age
    30
    Posts
    7
    Reputation
    0

    Default

    Thanks for the improvement.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts