Sign in to follow this  
chbrules

[web] [ASP] Searching through an array

Recommended Posts

chbrules    170
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
evolutional    1393

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
pragma Fury    343
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
evolutional    1393
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
smr    2468
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this