Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > ASP.NET > ASP.NET General Discussion

ASP.NET General Discussion General discussion on ASP.NET development with VB.NET

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-26-2009, 2:40 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Age: 29
Posts: 5
Reputation: 0
Coffer is on a distinguished programming path ahead
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-26-2009, 4:03 PM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 816
Reputation: 459
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
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))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-26-2009, 4:48 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Age: 29
Posts: 5
Reputation: 0
Coffer is on a distinguished programming path ahead
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-26-2009, 5:52 PM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 816
Reputation: 459
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
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"))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-26-2009, 7:05 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Age: 29
Posts: 5
Reputation: 0
Coffer is on a distinguished programming path ahead
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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 06-26-2009, 9:05 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,173
Reputation: 1273
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
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)
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-27-2009, 3:49 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Age: 29
Posts: 5
Reputation: 0
Coffer is on a distinguished programming path ahead
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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-27-2009, 5:10 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,173
Reputation: 1273
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
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
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-27-2009, 5:13 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Age: 29
Posts: 5
Reputation: 0
Coffer is on a distinguished programming path ahead
Default

Thanks for the improvement.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 8:23 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.