We help IT Professionals succeed at work.

Need to convert an ASP string value into a variable name

GrrWolfie
GrrWolfie asked
on
This is for my website...

Dim A, B
A = "fruit"
B = "pear"
(I need to convert the value of A into a variable name and give it the value of B)
If (fruit = "pear") Then
   Response.Write("success!")
End If

Can't find this anywhere, closest I could find was someone mentioning reflection with a link to here: http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx

Can anyone help?
Comment
Watch Question

you cant convert string you need to still use a and b as references

Dim A, B
A = "fruit"
B = "pear"
(I need to convert the value of A into a variable name and give it the value of B)
If (a= b) Then
   Response.Write("success!")
End If
 
you also need to write another statement when its not equal so it can show you whether your logic is working or not

Author

Commented:
I'll explain the concept further. Here's an example of what I want to do using Session variables:

Dim A, B
A = "fruit"
B = "pear"
Session(A) = B
If (Session("fruit") = "pear") Then
   Response.Write("success!")
End If
--- Webpage output: success! ---

I need to do this without using Session variables.
ok there questions here
does A remain  same all the times
does B remain same all the times
if they change what triggers that seems to me if you are doing some sort of logic based on dynamic values if they are static then you dont need this code

Author

Commented:
The code above was created to simplify the question, what i'm doing is looping through a database of login names which need to be turned into variables so I can assign values to them. I'm not interested in a work around as I know code exists to solve this problem.
What in the end are you trying to accomplish?

Author

Commented:
"looping through a database of login names which need to be turned into variables so I can assign values to them."
Why not use a collection?   Put items in the collection with name as key, and value associated with key.

Author

Commented:
After a lot of browsing I became extremely disappointed with the cander of ASP community. I saw no examples. Finally only a reference to a code (no example) only after the poster had verbal buffeted the person who asked for it. In every forum I saw pages and pages of people obsessed with finding fault in the programmers design rather than offering the answer (they could always offer advice AFTER the answer). All of these threads but one ended with an unanswered question still hanging. Pathetic. Perhaps no one knew the answer in those threads but they could at least had the decency to say so rather than stringing the poor posters along.

Well here it is...
<%
Dim A, B, C
A = "fruit"
B = "pear"
C = A & "=B"
Execute(C)
If (fruit = "pear") Then
   Response.Write("success!")
Else
   Response.Write("fruit = >" & fruit & "<")
End If
%>

Tip:
Code within the Execute() call must be a complete string. Execute(A & "=B") will not work.

Resources:
http://www.4guysfromrolla.com/webtech/030300-1.shtml   <-- Execute usage examples
Doesn't show an example of actual variable creation from a value but it gave me enough clues to do so.

Author

Commented:
Sorry if I came off a bit harsh, it's just rough seeing so many people with the same unanswered question only faced with backing up their decisions thinking they'll get to the answer that way because no one manned up to saying they don't have it.

Author

Commented:
lloydswayze, that technique sounds interesting as an alternative that does the same thing,

how do you put items in a collection with name as key?
I gather that you are not using dot.net.  

If you were you just define a collection with VB .Net it would be like this

    Dim oDD As New NameValueCollection
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        oDD.Add("key1", "value1")
        oDD.Add("key2", "value2")
        Dim xx As String
        Dim keyval As String
        keyval = "key1"
        xx = oDD(keyval)
        Response.Write(keyval + "   " + xx)
    End Sub

The output from the "Repsonse.Write" line was
    key1  value1

I'm not sure what was available for ASP (not ASPX)
if you can't find it available  a 2 dimensional array will do the trick

        Dim maxDD2 As Integer
        maxDD2 = 10
        Dim oDD2(maxDD2, 2) As String
        Dim topDD2 As Integer
        topDD2 = 0

        REM  Add item
        If topDD2 < maxDD2 Then
            topDD2 = topDD2 + 1
        End If
        oDD2(topDD2, 1) = "key1"
        oDD2(topDD2, 2) = "value1"

        REM Search for keyval
        For i = 1 To maxDD2
            If oDD2(i, 1) = keyval Then
                xx = oDD2(i, 2)
                Response.Write("------> " + keyval + "   " + xx)
            End If
        Next

if you have large amounts of data in the array, then a sort and search algorithm would increase its performance.  For a few hundred records, you should notice the difference.


Incidently, using the Execute method can be dangerous if you don't have complete control of the data feeding it.  For instance a quote or special character might cause it to create a syntax error.   Its something like, but not nearly as dangerous, as using assembly code to change future code, if you mess up, it blows up.
Good idea but the irritating thing there is the necessity for loops to "find" items instead of just being able to declare them and request their value. I think i've found the definitive answer which does away with the need for the Execute command. It's using an ASP Dictionary Object. Essential all the benefits of creating stand-alone variables but the added benefit of being able to loop through the contents.

Dim A, B
A = "fruit"
B = "pear"
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add A, B
If (d.item(A) = B) Then
   Response.Write("success!")
End If
--- Webpage output: success! ---
(Once again... fruit has become a variable that holds a value :) )

Resources:
http://www.w3schools.com/asp/asp_ref_dictionary.asp <-- Everything you need.

Author

Commented:
Thankyou lloyd btw, your NameValueCollection script lead me to the Dictionary Object. :)