[thelist] VBScript - declaring dynamically named vars in a Forloop

Ken Schaefer Ken at adOpenStatic.com
Thu Mar 3 18:51:12 CST 2005


a) Have you considered using a Dictionary component rather than an array?
That way you can use key/values rather than indices - generally easier to
manage in these scenarios

b) I would not consider the code you have below to be particularly good. You
run the risk of nasty errors should you fail to be able to do the cast.
Remember: defensive programming. Don't blame Microsoft for writing insecure,
buggy code :-)

c) The Request.Form collection can be enumerated. That makes it easy to stuff
all your elements into an array or dictionary:

<%
For Each objItem in Request.Form

	Response.Write(objItem & " = " & Request.Form(objItem) & "<br />" &
vbCrLf)

Next
%>

You can get a count of items in the collection, or a subset of the
collection:

<%
i = Request.Form.Items.Count()

i = Request.Form("someField").Items.Count()
%>

You can filter on specific field names by prepending some kind of static
text:

<%
For Each objItem in Request.Form

	If Left(objItem.Name, 7) = "visible" then

		Response.Write(Request.Form(objItem)

	Next

Next
%>


Lots of good information here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/
ef3a75f3-aaff-4f0e-b52f-3206c0886476.asp

Cheers
Ken

: -----Original Message-----
: From: thelist-bounces at lists.evolt.org [mailto:thelist-
: bounces at lists.evolt.org] On Behalf Of Pringle, Ron
: Sent: Friday, 4 March 2005 8:20 AM
: To: 'thelist at lists.evolt.org'
: Subject: RE: [thelist] VBScript - declaring dynamically named vars in a
: Forloop
: 
: > Ok, backing up here. How to I declare an array with a value
: > that is passed
: > as a variable from a form?
: >
: > Assuming I have a text input form where a number is entered,
: > and that form
: > field is named "howMany". Lets say I've input "2" in that
: > field, so using
: > the below code:
: >
: > howMany = CInt(request.form("howMany"))
: >
: > howMany should have an integer of 2 as its value, correct?
: >
: > Now I want to dimension an array with the size of "howMany".
: > I've tried
: > writing it out as:
: >
: > Dim sEventDate(howMany)
: >
: > That just throws an error saying "Expected integer constant".
: 
: Ok, I kind of get it now. I've done the following:
: 
: 
: Dim seventDate()
: ReDim seventDate(CInt(request.form("howMany")))
: 
: Now I just need to figure out how to fill the array with the contents of
: the
: form fields.
: 
: TY for the help all, I'm getting my head around it slowly.
: 
: Ron
: --
: 
: * * Please support the community that supports you.  * *
: http://evolt.org/help_support_evolt/
: 
: For unsubscribe and other options, including the Tip Harvester
: and archives of thelist go to: http://lists.evolt.org
: Workers of the Web, evolt !


More information about the thelist mailing list