Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP - confirm variable exists - dynamically

Status
Not open for further replies.

Wickersty

Programmer
Nov 13, 2002
51
US
Hi all,

Somewhat new to ASP. I have a script that needs to be prepared for any number of grade_# variables... for example:

grade_1
grade_2
...
grade_n

Simple psuedo-code is:

Dim i
i=1

While grade_i exists
' do something
i=i+1
Wend

I'm not sure how to
a) make asp treat grade_i as a variable (as in grade_1 when i=1, etc)
b) make ASP confirm that grade_i (grade_1, grade_2, etc) exists...

Any help would be appreciated...

Thanks...

Jeff
 
Use an array for your variables and ReDim Preserve to add elements as needed.
 
That's probably not going to be an option, which i won't explain for sake of time other than to say the incoming data is out of my control... I just have to deal with it... Any other suggestions?

I'd at least have a head start if someone could advise how to get ASP to look for "grade_" + i as a variable (ie the variable grade_1 when i=1).

Thanks again
 
Try this
Code:
While grade_(i) exists
   ' do something
   i=i+1
Wend
Not 100% sure, but I think it will do what you want.
 
I don't think that will work, but hey, i've been surprised before. The exists will work great if you hard code each variable.

Looks like bad design if you have a dynamic # of grades being staticly assigned. As they are static, so will need to be your code.

I'd fight for a dynamic array as input. I really don't see how else you could get around hard coding checking each variable. That's why array's exist is to get around this problem.

Good luck though.
 
there isn't that many ways that data can be transfered in so using the request collection.
form for example

Code:
dim x
dim counter
dim Pos
dim grade

for each x in request.form
' retrieve option index number from form collection

if lcase(left(x,6)) = "grade_" then
Pos = InStrRev(x,"_")
grade = cint(right(x,len(x)-Pos))
' do the processing for grade value here
counter = counter + 1
end if

next


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
Nightclub counting systems

So long, and thanks for all the fish.
 
In what format is the data set that you are handling? How is it piped to your application? (Where does it come from?) What exactly IS grade_#? Is it an object, a variable, what?

Just curious as to why these set of questions aren't as equaly important to helping you find a solution.
 
hmm... that's true, if you are getting the data from a form or querystring, then you should have no trouble looping through it checking variables.

I was assuming the variables would be defined in the current asp script.

More info.
 
If you absolutely have to look for a series of variables like tat, that may or may not exist, you could create a mall function that would do it like so:
Code:
Function isVar(varName)
   On Error Resume Next
   Dim t
   t = Execute(varName)
   isVar = (err.no = 0)
   On Error Goto 0
End Function
Basically you would just pass the hopeful variable name to this function as a string. The Execute function would evaluate that string as code, attempting to retrieve a variable from it.

Now, mythoughts on this matter are that if you can do it with numbered variables you can do it with an array. If this is coming from the Request collections, it can be placed into an array. If it is coming fom a database (or any ADO object), it can be placed into an array. If it is coming from a flat file (using FSO), it can be placed into an array. The only thing that could possibly force you to use numbered variables would be a pre-built include file. If thats the case it may bea good idea to consider firing the persoin who wrote it before they hurt you any further.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top