converting string to variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • senweb
    New Member
    • Jun 2010
    • 2

    #1

    converting string to variable

    dim a_1, a_2, a_3,i
    a_1=x
    a_2=y
    a_3=z

    for i=1 to 3
    response.write( a_ & i)
    next


    result should be: xyz

    But the expected result is not coming.
    there is an error in response.write( )


    pls. help me to solve it.

    note: i want ASP coding
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Think it should be more like
    Code:
    response.write('a_' + i)

    Comment

    • viktorka
      New Member
      • Jun 2010
      • 26

      #3
      You cannot pass a name of a variable as string.
      Why don't you use array:

      dim a()
      dim i
      redim a(i)

      a(0)=x
      a(1)=y
      a(2)=z

      for c=0 to i
      response.write( a(count))
      next

      Comment

      • viktorka
        New Member
        • Jun 2010
        • 26

        #4
        dim a()
        dim i
        i = 2
        redim a(i)


        a(0)=x
        a(1)=y
        a(2)=z

        for c=0 to i
        response.write( a(count))
        next

        Comment

        • viktorka
          New Member
          • Jun 2010
          • 26

          #5
          Sorry, another typo

          dim a()
          dim i
          i = 2
          redim a(i)


          a(0)=x
          a(1)=y
          a(2)=z

          for c=0 to i
          response.write( a(c))
          next

          Comment

          • jhardman
            Recognized Expert Specialist
            • Jan 2007
            • 3405

            #6
            Viktorka's solution is the best, but this will also work (I use a similar approach for form inputs all the time):
            Code:
            session("a_1") = x
            session("a_2") = y
            session("a_3") = x
            
            for x = 0 to 2
               response.write session("a_"+x)
            next
            Jared

            Comment

            Working...