referring to contents of a variable using the text of the variable name? 
Author Message
 referring to contents of a variable using the text of the variable name?

I'm thinking the following must be something simple that I just can't
stumble upon.

Is there a way to refer to the contents of a variable in server-side script
using the text of the variable name?  I can do this in client-side scripting
using the document.all object collection, but I don't know the equivalent on
the server-side.   The following shows what I'm trying to do.  I'm trying to
re-use the same validation subroutine to check multiple variables.

Thanks for any help.
Mike Sides
Director of IT
Verilll & Dana
Portland, Maine

--------------------------------

<%
dim  myvariable1
dim  myvariable2
dim  myvariable3
dim  myvariable4

validate("myvariable1")
validate("myvariable2")
validate("myvariable3")
validate("myvariable4")

sub validate(v)
    'v contains the name of variable i want to validate (myvariable)
    'how to I refer to the contents of variable V at this point?  somthing
like?
    response.write(v)    'displays myvariable

    response.write (variables(v).contents)  'this doesn't work
    response.write (document.all(v).value) 'this doesn't work either since
there's
                                                               'no document
object to refer to on the server-side script
end sub
%>



Sat, 17 Dec 2005 04:25:02 GMT  
 referring to contents of a variable using the text of the variable name?
Why bother? Just pass the variable itself to the sub...

The document.all collection is not a collection of variables: it is a
collection of objects. All collections are keyed by a text string.

Variables are not put into any collection: variables are merely pointers to
a space in memory containing a piece of data. There is no way to loop
through or enumerate all the variables that you have declared.

In your example, the Validate sub does not need to know the name of the
variable: it is receiving the value of the variable in the call to the sub.

sub validate(v)
    response.write v & " is valid"
end sub

dim thisvar, thisvar1
thisvar = "a"
thisvar1 = "b"
validate thisvar
validate thisvar1

HTH,
Bob Barrows

Quote:

> I'm thinking the following must be something simple that I just can't
> stumble upon.

> Is there a way to refer to the contents of a variable in server-side
> script using the text of the variable name?  I can do this in
> client-side scripting using the document.all object collection, but I
> don't know the equivalent on the server-side.   The following shows
> what I'm trying to do.  I'm trying to re-use the same validation
> subroutine to check multiple variables.

> Thanks for any help.
> Mike Sides
> Director of IT
> Verilll & Dana
> Portland, Maine

> --------------------------------

> <%
> dim  myvariable1
> dim  myvariable2
> dim  myvariable3
> dim  myvariable4

> validate("myvariable1")
> validate("myvariable2")
> validate("myvariable3")
> validate("myvariable4")

> sub validate(v)
>     'v contains the name of variable i want to validate (myvariable)
>     'how to I refer to the contents of variable V at this point?
> somthing like?
>     response.write(v)    'displays myvariable

>     response.write (variables(v).contents)  'this doesn't work
>     response.write (document.all(v).value) 'this doesn't work either
> since there's
>                                                                'no
> document object to refer to on the server-side script
> end sub
> %>



Sat, 17 Dec 2005 04:51:26 GMT  
 referring to contents of a variable using the text of the variable name?
The reason I'm trying to do this, is that I have 28 variables to validate
and I want to set other variables based on the name of of the variable I
happen to be checking such as:

validate("v1")
validate("v2")
validate("v3")
validate("v4")

sub validate(v)
    'check original variable
    if variable(v).value=1  then   'again showing a hypothetical reference
to the variable using the variable name
        variable(v&"error") = "ok"
    else
        variable(v&"error") = "bad"
    end if
end sub

With this code, I would get 4 new variables called

v1error
v2error
v3error
v4error

Which would either be set to "ok" or "bad" depending on whether the original
variable was validated or not.

But, as you say, if variables aren't stored in any type of collection, I'm
S-O-L trying to do this at all.  I'll find another way.   Thanks very much
for your help.

Quote:
---- Original Message -----

Newsgroups: microsoft.public.inetsdk.programming.scripting.VBScript
Sent: Monday, June 30, 2003 4:51 PM
Subject: Re: referring to contents of a variable using the text of the
variable name?

> Why bother? Just pass the variable itself to the sub...

> The document.all collection is not a collection of variables: it is a
> collection of objects. All collections are keyed by a text string.

> Variables are not put into any collection: variables are merely pointers
to
> a space in memory containing a piece of data. There is no way to loop
> through or enumerate all the variables that you have declared.

> In your example, the Validate sub does not need to know the name of the
> variable: it is receiving the value of the variable in the call to the
sub.

> sub validate(v)
>     response.write v & " is valid"
> end sub

> dim thisvar, thisvar1
> thisvar = "a"
> thisvar1 = "b"
> validate thisvar
> validate thisvar1

> HTH,
> Bob Barrows


> > I'm thinking the following must be something simple that I just can't
> > stumble upon.

> > Is there a way to refer to the contents of a variable in server-side
> > script using the text of the variable name?  I can do this in
> > client-side scripting using the document.all object collection, but I
> > don't know the equivalent on the server-side.   The following shows
> > what I'm trying to do.  I'm trying to re-use the same validation
> > subroutine to check multiple variables.

> > Thanks for any help.
> > Mike Sides
> > Director of IT
> > Verilll & Dana
> > Portland, Maine

> > --------------------------------

> > <%
> > dim  myvariable1
> > dim  myvariable2
> > dim  myvariable3
> > dim  myvariable4

> > validate("myvariable1")
> > validate("myvariable2")
> > validate("myvariable3")
> > validate("myvariable4")

> > sub validate(v)
> >     'v contains the name of variable i want to validate (myvariable)
> >     'how to I refer to the contents of variable V at this point?
> > somthing like?
> >     response.write(v)    'displays myvariable

> >     response.write (variables(v).contents)  'this doesn't work
> >     response.write (document.all(v).value) 'this doesn't work either
> > since there's
> >                                                                'no
> > document object to refer to on the server-side script
> > end sub
> > %>



> Why bother? Just pass the variable itself to the sub...

> The document.all collection is not a collection of variables: it is a
> collection of objects. All collections are keyed by a text string.

> Variables are not put into any collection: variables are merely pointers
to
> a space in memory containing a piece of data. There is no way to loop
> through or enumerate all the variables that you have declared.

> In your example, the Validate sub does not need to know the name of the
> variable: it is receiving the value of the variable in the call to the
sub.

> sub validate(v)
>     response.write v & " is valid"
> end sub

> dim thisvar, thisvar1
> thisvar = "a"
> thisvar1 = "b"
> validate thisvar
> validate thisvar1

> HTH,
> Bob Barrows


> > I'm thinking the following must be something simple that I just can't
> > stumble upon.

> > Is there a way to refer to the contents of a variable in server-side
> > script using the text of the variable name?  I can do this in
> > client-side scripting using the document.all object collection, but I
> > don't know the equivalent on the server-side.   The following shows
> > what I'm trying to do.  I'm trying to re-use the same validation
> > subroutine to check multiple variables.

> > Thanks for any help.
> > Mike Sides
> > Director of IT
> > Verilll & Dana
> > Portland, Maine

> > --------------------------------

> > <%
> > dim  myvariable1
> > dim  myvariable2
> > dim  myvariable3
> > dim  myvariable4

> > validate("myvariable1")
> > validate("myvariable2")
> > validate("myvariable3")
> > validate("myvariable4")

> > sub validate(v)
> >     'v contains the name of variable i want to validate (myvariable)
> >     'how to I refer to the contents of variable V at this point?
> > somthing like?
> >     response.write(v)    'displays myvariable

> >     response.write (variables(v).contents)  'this doesn't work
> >     response.write (document.all(v).value) 'this doesn't work either
> > since there's
> >                                                                'no
> > document object to refer to on the server-side script
> > end sub
> > %>



Sat, 17 Dec 2005 05:18:28 GMT  
 referring to contents of a variable using the text of the variable name?
I'm still not quite clear on your rationale, but my suggestion would be to
use an array, or a dictionary object, which is the closest thing you'll find
to a collection in vbscript.

Bob Barrows

Quote:

> The reason I'm trying to do this, is that I have 28 variables to
> validate and I want to set other variables based on the name of of
> the variable I happen to be checking such as:

> validate("v1")
> validate("v2")
> validate("v3")
> validate("v4")

> sub validate(v)
>     'check original variable
>     if variable(v).value=1  then   'again showing a hypothetical
> reference to the variable using the variable name
>         variable(v&"error") = "ok"
>     else
>         variable(v&"error") = "bad"
>     end if
> end sub

> With this code, I would get 4 new variables called

> v1error
> v2error
> v3error
> v4error

> Which would either be set to "ok" or "bad" depending on whether the
> original variable was validated or not.

> But, as you say, if variables aren't stored in any type of
> collection, I'm S-O-L trying to do this at all.  I'll find another
> way.   Thanks very much for your help.

> ---- Original Message -----

> Newsgroups: microsoft.public.inetsdk.programming.scripting.vbscript
> Sent: Monday, June 30, 2003 4:51 PM
> Subject: Re: referring to contents of a variable using the text of the
> variable name?

>> Why bother? Just pass the variable itself to the sub...

>> The document.all collection is not a collection of variables: it is a
>> collection of objects. All collections are keyed by a text string.

>> Variables are not put into any collection: variables are merely
>> pointers to a space in memory containing a piece of data. There is
>> no way to loop through or enumerate all the variables that you have
>> declared.

>> In your example, the Validate sub does not need to know the name of
>> the variable: it is receiving the value of the variable in the call
>> to the sub.

>> sub validate(v)
>>     response.write v & " is valid"
>> end sub

>> dim thisvar, thisvar1
>> thisvar = "a"
>> thisvar1 = "b"
>> validate thisvar
>> validate thisvar1

>> HTH,
>> Bob Barrows


>>> I'm thinking the following must be something simple that I just
>>> can't stumble upon.

>>> Is there a way to refer to the contents of a variable in server-side
>>> script using the text of the variable name?  I can do this in
>>> client-side scripting using the document.all object collection, but
>>> I don't know the equivalent on the server-side.   The following
>>> shows what I'm trying to do.  I'm trying to re-use the same
>>> validation subroutine to check multiple variables.

>>> Thanks for any help.
>>> Mike Sides
>>> Director of IT
>>> Verilll & Dana
>>> Portland, Maine

>>> --------------------------------

>>> <%
>>> dim  myvariable1
>>> dim  myvariable2
>>> dim  myvariable3
>>> dim  myvariable4

>>> validate("myvariable1")
>>> validate("myvariable2")
>>> validate("myvariable3")
>>> validate("myvariable4")

>>> sub validate(v)
>>>     'v contains the name of variable i want to validate (myvariable)
>>>     'how to I refer to the contents of variable V at this point?
>>> somthing like?
>>>     response.write(v)    'displays myvariable

>>>     response.write (variables(v).contents)  'this doesn't work
>>>     response.write (document.all(v).value) 'this doesn't work either
>>> since there's
>>>                                                                'no
>>> document object to refer to on the server-side script
>>> end sub
>>> %>



>> Why bother? Just pass the variable itself to the sub...

>> The document.all collection is not a collection of variables: it is a
>> collection of objects. All collections are keyed by a text string.

>> Variables are not put into any collection: variables are merely
>> pointers to a space in memory containing a piece of data. There is
>> no way to loop through or enumerate all the variables that you have
>> declared.

>> In your example, the Validate sub does not need to know the name of
>> the variable: it is receiving the value of the variable in the call
>> to the sub.

>> sub validate(v)
>>     response.write v & " is valid"
>> end sub

>> dim thisvar, thisvar1
>> thisvar = "a"
>> thisvar1 = "b"
>> validate thisvar
>> validate thisvar1

>> HTH,
>> Bob Barrows


>>> I'm thinking the following must be something simple that I just
>>> can't stumble upon.

>>> Is there a way to refer to the contents of a variable in server-side
>>> script using the text of the variable name?  I can do this in
>>> client-side scripting using the document.all object collection, but
>>> I don't know the equivalent on the server-side.   The following
>>> shows what I'm trying to do.  I'm trying to re-use the same
>>> validation subroutine to check multiple variables.

>>> Thanks for any help.
>>> Mike Sides
>>> Director of IT
>>> Verilll & Dana
>>> Portland, Maine

>>> --------------------------------

>>> <%
>>> dim  myvariable1
>>> dim  myvariable2
>>> dim  myvariable3
>>> dim  myvariable4

>>> validate("myvariable1")
>>> validate("myvariable2")
>>> validate("myvariable3")
>>> validate("myvariable4")

>>> sub validate(v)
>>>     'v contains the name of variable i want to validate (myvariable)
>>>     'how to I refer to the contents of variable V at this point?
>>> somthing like?
>>>     response.write(v)    'displays myvariable

>>>     response.write (variables(v).contents)  'this doesn't work
>>>     response.write (document.all(v).value) 'this doesn't work either
>>> since there's
>>>                                                                'no
>>> document object to refer to on the server-side script
>>> end sub
>>> %>



Sat, 17 Dec 2005 06:56:44 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. RecNo function

2. Using variable to refer to variable name?

3. How do I use contents of variable as field name(db!rst!variable)

4. building a string into a variable then text variable as a control name

5. Referring to Field Names with Variables

6. How do I refer to an element of a collection by NAME with a variable

7. Refer to a member of User Defined Type by using a Variable's Value

8. How to refer to a form in VBA using a variable

9. Referring to a variableName using another variable?

10. Refer to field using a variable

11. Assign variable contents to another variable

12. reference contents of variable as a variable...

 

 
Powered by phpBB® Forum Software