After Parenthesis in ASP Variables - Query

Discussion in 'ASP General' started by ll, May 27, 2008.

  1. ll

    ll Guest

    Hi,
    I'm wondering about variable declaration in ASP - is there a good
    resource for this? In naming variables, is there a way to include
    characters in a variable name after the parenthesis/variable in a
    loop? I have quite a few variables (e.g. strOut1_a, strOut2_a, etc)
    and thought a loop would be the best way to do this, but I am getting
    errors for the characters after the parenthesis. My code is below.
    Thanks for any help or resources.
    Louis

    -----------------------------------------
    for a=1 to 3
    strOut(a)_a=objComm("Out"&a&"_a")
    response.write("Out"&a&"_a")
    next
    ll, May 27, 2008
    #1
    1. Advertising

  2. ll

    Drew Guest

    Use an array,

    dim a, arrStrOut(2) 'Remember arrays are zero based
    for a = 0 to UBound(arrStrOut)
    arrStrOut(a) = a
    response.write(arrStrOut(a))
    next

    Drew

    "ll" <> wrote in message
    news:...
    > Hi,
    > I'm wondering about variable declaration in ASP - is there a good
    > resource for this? In naming variables, is there a way to include
    > characters in a variable name after the parenthesis/variable in a
    > loop? I have quite a few variables (e.g. strOut1_a, strOut2_a, etc)
    > and thought a loop would be the best way to do this, but I am getting
    > errors for the characters after the parenthesis. My code is below.
    > Thanks for any help or resources.
    > Louis
    >
    > -----------------------------------------
    > for a=1 to 3
    > strOut(a)_a=objComm("Out"&a&"_a")
    > response.write("Out"&a&"_a")
    > next
    Drew, May 27, 2008
    #2
    1. Advertising

  3. ll wrote:
    > Hi,
    > I'm wondering about variable declaration in ASP - is there a good
    > resource for this?


    I can see why you had trouble finding one, given that you were treating
    ASP as a language rather than what it really is: a "platform" which
    supports several scripting languages, including vbscript. I'm sure if
    you knew you had to find a vbscript reference you would have found this
    http://msdn.microsoft.com/en-us/library/t0aew7h6(VS.85).aspx
    or this:
    http://www.microsoft.com/downloads/...48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en

    > In naming variables, is there a way to include
    > characters in a variable name after the parenthesis/variable in a
    > loop?


    After the parentheses? No. I'm not sure there is a language where this
    is allowed. Certainly not VB, VBA or vbscript. With vbscript, the
    parentheses contain, when declaring the array, the maximum index value
    to be stored in the array, and when referring to an item within the
    array, the index of the item.

    Have a look in the documentation about declaring and using arrays in
    vbscript.


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], May 27, 2008
    #3
  4. ll wrote:
    > I'm wondering about variable declaration in ASP - is there a good
    > resource for this? In naming variables, is there a way to include
    > characters in a variable name after the parenthesis/variable in a
    > loop? I have quite a few variables (e.g. strOut1_a, strOut2_a, etc)
    > and thought a loop would be the best way to do this, but I am getting
    > errors for the characters after the parenthesis. My code is below.
    > Thanks for any help or resources.
    > Louis
    >
    > -----------------------------------------
    > for a=1 to 3
    > strOut(a)_a=objComm("Out"&a&"_a")
    > response.write("Out"&a&"_a")
    > next


    VBScript allows all kinds of funny variable names, provided you use
    brackets. This is a valid expression:

    [strOut(a)_a] = objComm("Out"&a&"_a")

    It is not what you want, however, since it is merely a static variable name.


    OTOH, you can probably get a little of what you want with JScript:

    for (var ary=[],i=0; i<3; i++) {
    ary.push({_a:eek:bjComm("Out"&a&"_a")}
    Response.Write(ary._a)
    }



    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms.
    Dave Anderson, May 27, 2008
    #4
  5. ll

    Old Pedant Guest

    I wonder if this is what you are after...

    First of all, there's no reason you couldn't put the suffix *BEFORE* the
    parentheses:
    -----------------------------------------
    for a=1 to 3
    strOut_a(a) = objComm("Out"&a&"_a")
    next

    But if you mean that you want *properties* on a SINGLE indexed variable of
    an array...sure, you can do that. Using a VBScript class.

    Silly (and not overly well formed) example, just to get the point across:

    <%
    Class Person
    Private mName, mEmail

    Public Sub Init( name, email )
    me.mName = name
    ne.mEmail = email
    End Sub

    Public Property Get Name( )
    Name = me.mName
    End Property
    Public Property Get EMail( )
    EMail = me.mEmail
    End Property
    End Class

    Dim people( 10 )

    Set people(3) = New Person
    people(3).Init( "Adam", "")
    Set people(7) = New Person
    people(7).Init("Joe","")

    ....
    x = 7

    Response.Write people(x).Name & " has email address " & people(x).Email
    ....
    %>

    Is *THAT* what you are after?
    Old Pedant, May 28, 2008
    #5
  6. Re: I wonder if this is what you are after...

    Old Pedant wrote:
    > First of all, there's no reason you couldn't put the suffix *BEFORE*
    > the parentheses:
    > -----------------------------------------
    > for a=1 to 3
    > strOut_a(a) = objComm("Out"&a&"_a")
    > next
    >
    > But if you mean that you want *properties* on a SINGLE indexed
    > variable of an array...sure, you can do that. Using a VBScript class.
    >
    > Silly (and not overly well formed) example, just to get the point
    > across:
    >


    Now that was more helpful than my reply was. Thanks a lot for stepping in.

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], May 28, 2008
    #6
  7. ll

    ll Guest

    Re: I wonder if this is what you are after...

    On May 28, 5:55 am, "Bob Barrows [MVP]" <>
    wrote:
    > Old Pedant wrote:
    > > First of all, there's no reason you couldn't put the suffix *BEFORE*
    > > the parentheses:
    > > -----------------------------------------
    > > for a=1 to 3
    > > strOut_a(a) = objComm("Out"&a&"_a")
    > > next

    >
    > > But if you mean that you want *properties* on a SINGLE indexed
    > > variable of an array...sure, you can do that. Using a VBScript class.

    >
    > > Silly (and not overly well formed) example, just to get the point
    > > across:

    >
    > Now that was more helpful than my reply was. Thanks a lot for stepping in.
    >
    > --
    > Microsoft MVP - ASP/ASP.NET
    > Please reply to the newsgroup. This email account is my spam trap so I
    > don't check it very often. If you must reply off-line, then remove the
    > "NO SPAM"




    Thanks for all your help - this has helped immensely!
    ll, May 29, 2008
    #7
  8. ll

    ll Guest

    Re: I wonder if this is what you are after...

    On May 29, 11:05 am, ll <> wrote:
    > On May 28, 5:55 am, "Bob Barrows [MVP]" <>
    > wrote:
    >
    >
    >
    > > Old Pedant wrote:
    > > > First of all, there's no reason you couldn't put the suffix *BEFORE*
    > > > the parentheses:
    > > > -----------------------------------------
    > > > for a=1 to 3
    > > > strOut_a(a) = objComm("Out"&a&"_a")
    > > > next

    >
    > > > But if you mean that you want *properties* on a SINGLE indexed
    > > > variable of an array...sure, you can do that. Using a VBScript class.

    >
    > > > Silly (and not overly well formed) example, just to get the point
    > > > across:

    >
    > > Now that was more helpful than my reply was. Thanks a lot for stepping in.

    >
    > > --
    > > Microsoft MVP - ASP/ASP.NET
    > > Please reply to the newsgroup. This email account is my spam trap so I
    > > don't check it very often. If you must reply off-line, then remove the
    > > "NO SPAM"

    >
    > Thanks for all your help - this has helped immensely!



    One more (hopefully quick) question... within the loops as shown in
    my code, does each variable created (e.g. strOutP1, strOutP2, etc)
    retain its value, or is an array needed for this?
    Thanks again,
    Louis
    ll, May 29, 2008
    #8
  9. ll

    Old Pedant Guest

    Mystery to me

    > One more (hopefully quick) question... within the loops as shown in
    > my code, does each variable created (e.g. strOutP1, strOutP2, etc)
    > retain its value, or is an array needed for this?


    Ummm...."loops"??? You only show ONE loop there.

    And you don't show *ANY* variables name strOutP1 or strOutP2 or anything
    similar to that.

    What in the heck are you talking about??? Maybe code you didn't show us?
    Old Pedant, Jun 1, 2008
    #9
  10. ll

    ll Guest

    Re: Mystery to me

    On Jun 1, 12:40 am, Old Pedant <>
    wrote:
    > > One more (hopefully quick) question... within the loops as shown in
    > > my code, does each variable created (e.g. strOutP1, strOutP2, etc)
    > > retain its value, or is an array needed for this?

    >
    > Ummm...."loops"??? You only show ONE loop there.
    >
    > And you don't show *ANY* variables name strOutP1 or strOutP2 or anything
    > similar to that.
    >
    > What in the heck are you talking about??? Maybe code you didn't show us?




    Sorry for the confusion - allow me to just start from the beginning,
    which might be easier. I have a set of variables
    (strOut1_a....to...strOut15_a).
    I'm looking for a way to loop through these variable names to
    establish their values, such as:

    strOut1_a = objComm("Out1_a")

    but with a loop, rather than writing out each.
    Many thanks again,
    Louis
    ll, Jun 2, 2008
    #10
  11. ll

    Old Pedant Guest

    Re: Mystery to me

    > Sorry for the confusion - allow me to just start from the beginning,
    > which might be easier. I have a set of variables
    > (strOut1_a....to...strOut15_a).
    > I'm looking for a way to loop through these variable names to
    > establish their values, such as:
    >
    > strOut1_a = objComm("Out1_a")
    >


    No, you do *NOT* want to do that. As a couple of people pointed out.

    You WANT TO USE AN ARRAY.

    Dim strOut_a(15)
    For i = 1 To 15
    strOut_a(i) = objComm("Out" & i & "_a")
    Next

    And then, where your *OTHER* code *WAS* using
    strOut7_a
    or whatever, you simply use
    strOut_a(7)

    What is wrong with that solution??

    Now, there *IS* a way to do what you claimed to want. But it is slow slow
    slow and clumsy and is not good programming practice and...well, the list
    goes on. But if we truly can't convince you to use an array--perhaps because
    some other component is expecting the indivdual names? if so, would you
    really want to use that component?--then:

    For i = 1 To 15
    Execute "strOut" & i & "_a = objComm(""Out" & i & "_a"")"
    Next

    Untested, but I think that's right.

    Ugly and slow code, though. Try hard to avoid it.
    Old Pedant, Jun 3, 2008
    #11
  12. Re: Mystery to me

    "ll" <> wrote in message
    news:...
    > On Jun 1, 12:40 am, Old Pedant <>
    > wrote:
    > > > One more (hopefully quick) question... within the loops as shown in
    > > > my code, does each variable created (e.g. strOutP1, strOutP2, etc)
    > > > retain its value, or is an array needed for this?

    > >
    > > Ummm...."loops"??? You only show ONE loop there.
    > >
    > > And you don't show *ANY* variables name strOutP1 or strOutP2 or anything
    > > similar to that.
    > >
    > > What in the heck are you talking about??? Maybe code you didn't show

    us?
    >
    >
    >
    > Sorry for the confusion - allow me to just start from the beginning,
    > which might be easier. I have a set of variables
    > (strOut1_a....to...strOut15_a).
    > I'm looking for a way to loop through these variable names to
    > establish their values, such as:
    >
    > strOut1_a = objComm("Out1_a")
    >
    > but with a loop, rather than writing out each.



    That can't be done, you need an array. I know thats already been said but
    things in this thread seemed to have got over complicated for some reason.

    Dim strOut_a(4)

    ' Stuff to assign values to the array elements


    For i = 0 To UBound(strOut_a)
    Response.Write strOut_a(i) & "<br />"
    Next




    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Jun 3, 2008
    #12
  13. ll

    Old Pedant Guest

    Actually, of course, it can be done...

    As I noted:

    <%
    For i = 1 To 15
    Execute "strOut" & i & "_a = objComm(""Out" & i & "_a"")"
    Next
    %>

    Or use ExecuteGlobal, of course.

    As I said, a crappy idea, but it does work.
    Old Pedant, Jun 3, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .Net Sports
    Replies:
    4
    Views:
    2,105
    Eliyahu Goldin
    Jun 29, 2005
  2. puzzlecracker
    Replies:
    0
    Views:
    438
    puzzlecracker
    Jan 25, 2006
  3. Rogue Noir
    Replies:
    6
    Views:
    2,414
    Arquebus257WeaMag
    Aug 2, 2003
  4. Bob Day
    Replies:
    1
    Views:
    306
    osmium
    Jul 11, 2003
  5. Replies:
    1
    Views:
    89
    -Lost
    Apr 4, 2007
Loading...

Share This Page