Jump to content
  • Advertisement
Sign in to follow this  

[web] [ASP] Searching through an array

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I manually created an array will 2 values for now. I was wondering how I can search through it to check for a value?
Dim sUsers(1)
sUsers(0) = "CBrinker"
sUsers(1) = "AWilson"
I already tried using:
check = Array.BinarySearch(sUsers, GetLoginName)
But I get this error:
Microsoft VBScript runtime error '800a01a8' 

Object required: 'Array' 

/marketing/product/index.asp, line 19 
Which is that line. Help? =/

Share this post


Link to post
Share on other sites
Advertisement

blnFound = False
For idx = 0 to UBound(sUsers)

if sUsers(idx) = GetLoginName Then
blnFound = True
Exit For
end if

Next

if blnFound = True Then
Response.Write "Found User at pos: " & idx
Else
Response.Write "user not found"
end if

Share this post


Link to post
Share on other sites
FYI: The reason your original code would not work is because Array.BinarySearch is an ASP.NET function.

And you would have needed to sort the array first for the BinarySearch method to work anyway.

Share this post


Link to post
Share on other sites
Are you not using a database of any kind? It'd probably be better than hardcoding your users into the code. If you have to hardcode, at least have a separate usr_const.asp file that you include at the start of your file, otherwise it could be a nightmare to maintain!

Share this post


Link to post
Share on other sites
There is no method called BinarySearch as far as I know. A binary search will only work on a sorted list anyway. Unless you are going to sort your list, the only way to do it is to iterate through all of the elements until you find what you're looking for. If it's sorted then you can write a binary search yourself, or find a code snippet somewhere and copy it.

This is all you can do with the array object:
users = new Array("patrick", "brandon")

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!