Previous Next

Thread: how to declare code in a variable?

Last post 05-09-2008 9:12 AM by tomkmvp. 6 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (7 items)

Sort Posts:

  • 04-30-2008, 8:09 PM

    • Maverick3D
    • Not Ranked
    • Joined on 05-01-2008, 12:03 AM
    • Australia
    • Posts 2

    how to declare code in a variable?

    I am currently learning ASP and am struggling to get any of the below scenarios to work and not sure how to correctly write the variables in order for them to work. Also would this be the best way to write this type of code or is there an easier, more effective way to do the same thing?

    Looking forward to your responses.

    Example 1 - website urls

    <%
    dim url1, url2, url3
    url1="http://www.google.com"
    url2="http://www.yahoo.com"
    url3="http://www.geocities.com"
    %>

    <% if url="google" then response.write(& url1) end if %>
    <% if url="yahoo" then response.write(& url2) end if %>
    <% if url="geocities" then response.write(& url3) end if %>


    Example 2 - css code

    <%
    dim div1, div2, div3
    div1="<div style='width:100px; height:100px; background:red;'>Red Box 100x100</div>"
    div2="<div style='width:100px; height:100px; background:blue;'>Blue Box 100x100</div>"
    div3="<div style='width:100px; height:100px; background:green;'>Green Box 100x100</div>"
    %>

    <% if url="red" then response.write(& div1) end if %>
    <% if url="blue" then response.write(& div2) end if %>
    <% if url="green" then response.write(& div3) end if %>

    Red Box 100x100
    " div2="
    Blue Box 100x100
    " div3="
    Green Box 100x100
    " %>

  • 04-30-2008, 11:42 PM In reply to

    Re: how to declare code in a variable?

    You could look at using Select Case statements instead of if / then's http://www.w3schools.com/VBScript/vbscript_conditionals.asp

    Your code doesn't look too bad, if it does the job and it's maintainable.  That is the most important thing, along with the proper amount of error coding, and comments on what it does.  That is my two cents.

    Steve Schofield
    Windows Server MVP - IIS
    http://weblogs.asp.net/steveschofield

    http://www.IISLogs.com
    Log archival solution
    Install, Configure, Forget
  • 05-01-2008, 9:00 AM In reply to

    • tomkmvp
    • Top 10 Contributor
    • Joined on 03-20-2003, 10:27 AM
    • Lawrenceville, NJ
    • Posts 3,775
    • IIS MVPs

    Re: how to declare code in a variable?

    If you're just learning ASP, why not go with the most recent technology in ASP.NET instead?

    My only comment on your code is not to use so many <% %> blocks, use instead:

    <%
    if url="google" then response.write(& url1) end if
    if url="yahoo" then response.write(& url2) end if
    if url="geocities" then response.write(& url3) end if
    %>

    Here's how you'd do the same with Select Case:

    Select Case url
       Case "google"
          Response.Write (& url1)
       Case "yahoo"
          Response.Write (& url2)
       Case "geocities"
          Response.Write (& url3)
    End Select

  • 05-01-2008, 9:25 PM In reply to

    Re: how to declare code in a variable?

    Tom, nice coding!  I second learning .NET.  It has been out for years, there are some great quickstarts and visual web developer express 2008 is a great place to start.

    http://www.microsoft.com/express/vwd/

    Steve Schofield
    Windows Server MVP - IIS
    http://weblogs.asp.net/steveschofield

    http://www.IISLogs.com
    Log archival solution
    Install, Configure, Forget
  • 05-02-2008, 8:32 AM In reply to

    • tomkmvp
    • Top 10 Contributor
    • Joined on 03-20-2003, 10:27 AM
    • Lawrenceville, NJ
    • Posts 3,775
    • IIS MVPs

    Re: how to declare code in a variable?

    steve schofield:
    visual web developer express 2008 is a great place to start.
    ... and best of all, IT"S FREE!

  • 05-09-2008, 6:43 AM In reply to

    • Maverick3D
    • Not Ranked
    • Joined on 05-01-2008, 12:03 AM
    • Australia
    • Posts 2

    Re: how to declare code in a variable?

    Thank you for your comments and suggestions. I have just started downloading the Visual Studio 2008 (ISO) file as suggested and will give this a crack.

    I was able to answer my own question:

    <% url1="http://www.google.com" %>
    <% if url="google" then response.write("Welcome to "&url1) end if  %>


    Just wondering why the statement fails when breaking over various lines eg, below?

    <%
    if url="google"
    then response.write("Welcome to "&url1)
    end if
    %>

  • 05-09-2008, 9:12 AM In reply to

    • tomkmvp
    • Top 10 Contributor
    • Joined on 03-20-2003, 10:27 AM
    • Lawrenceville, NJ
    • Posts 3,775
    • IIS MVPs

    Re: how to declare code in a variable?

    "then" needs to go on the same line as "if".

Page 1 of 1 (7 items)
Page view counter