0

In vbScript, the code:

For Each Key In Request.QueryString

will treat Request.QueryString like a collection and you can iterate through it. But the code:

mysite.com?<%=Request.QueryString%>

(ignoring sanitizing for now) treats Request.QueryString like a string with ampersands and equal signs.

My question: is it possible to create an object that behaves just like this, or is this unique to Request?

Thank you. I'm trying to write a wrapper object to sanitize the two Request collections (QueryString and Form) and it would be easier if I could include this feature.

(It was suggested that a prior question about building dictionaries may have related to my issue. It does not. That article talks about how to build a dictionary, but nowhere does it address my question, which is: given a dictionary created inside an object, how can you get a reference to the dictionary (NOT a particular element) to produce a string (like Request.Form, which will produce something like "var1=valone&var2=valtwo" if you use Response.Write(Request.Form) but will produce "valtwo" if you use Response.Write(Request.Form("var2")). And I don't mean is there some way to manipulate vbScript to extract such a string. I mean literally, if you reference the dictionary (without parentheses) how can you make that produce a string the way Request.Form (or Request.QueryString) does? There is nothing about this in that post. (At the risk of sending somebody else off on another unhelpful (no offense meant) tangent, let me repeat -- I'm trying to wrap Request in an object (to permit sanitizing) and it would be nice if I could make the wrapper object behave like Request itself in this particular aspect. I don't think it is possible, but first you need to understand what I'm asking for.))

user1693404
  • 173
  • 1
  • 12
  • There is no built-in method for doing this but you could create your own `Class` and implement the collection logic yourself but that would be a lot of work for very little gain. – user692942 Jun 30 '23 at 05:10
  • Does this answer your question? [How to create collection object in vbscript?](https://stackoverflow.com/questions/2864499/how-to-create-collection-object-in-vbscript) – user692942 Jun 30 '23 at 05:19
  • Also, you could utilise a `Scripting.Dictionary` to do the collection work for you and build a method that does the string building on top of it. – user692942 Jun 30 '23 at 09:07
  • I'm sorry to say no, it doesn't answer my question. My question is: can you create an object (a class) that works the way Request does, i.e., have a property acting like a string in one context and like a collection in a different one? – user1693404 Jun 30 '23 at 19:58
  • @user692942 -- actually it's odd that you say "there is no built-in method for doing this" when actually, that is precisely my point, that there IS a "built-in" method, called "Request", which I am trying to emulate (by wrapping it with logic that permits me to sanitize its values). Then you say "You could create your own Class..." which is ALSO precisely my point -- HOW do you do that??? How do you create a Class that has a property which will act like a string in one context and like a collection in another. I.e., how WOULD you "create your own" with this same ability to override a property? – user1693404 Jun 30 '23 at 22:54
  • It's not odd the `Request` object's purpose is more than just allowing you to view a collection as a querystring. It is simply a feature of that "implementation", so I'll reiterate that there is no built-in way to re-create this functionality in your collections. The only option is to build a collection class, whether a full collection implementation using an array to back it or borrow from a COM library like `Scripting.Dictionary` it will still require you to build your own implementation. – user692942 Jul 01 '23 at 10:08
  • Does this answer your question? [How do I make my custom class compatible with For Each?](https://stackoverflow.com/a/30184195) – user692942 Jul 01 '23 at 10:10
  • Does this answer your question? [Lists in VBScript](https://stackoverflow.com/a/13609954) (Gives an example of a custom list class) – user692942 Jul 01 '23 at 10:14
  • @user1693404 You can't do this with pure VBScript because there are too many COM tricks going on in the background that you can't do with simple classes. You will eventually need to write a COM / ActiveX component. – Kul-Tigin Jul 16 '23 at 19:13

0 Answers0