ASP Code
  Home arrow ASP Code arrow Page 3 - ASP: The Beginning
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP CODE

ASP: The Beginning
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2007-11-20

    Table of Contents:
  • ASP: The Beginning
  • Variables
  • Working with Variables
  • Naming Conventions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP: The Beginning - Working with Variables


    (Page 3 of 4 )

    So now that all of that is out of the way, let's get our hands dirty with a little code. In this first example I will beat you until you learn the proper way to declare a variable:


    <html>

    <body>


    <%

    Dim teacher

    teacher=”Mister Kotter”


    response.write(“Welcome back “ & teacher)

    response.write(“Welcome back, welcome back, welcome back”)

    %>

    The above code displays the following text in the users browser:

      Welcome back Mister Kotter

      Welcome back, welcome back, welcome back

    The above code creates a variable named, “teacher” and stores the value “Mister Kotter” in it. We then use response.write to write the sentence “Welcome back” to the monitor, and use “&” plus the variable name to append the data within the variable to the sentence.

    Arrays

    Another way to think of a variable is to think of a file folder that has some information in it. You can take that data out, read it, put it back, or put other data into it; the information in it varies. Now when you store those file folders, you place them inside of a filing cabinet. In ASP (and pretty much every programming language), you store multiple pieces of data in arrays. Here is how you do it:


    <html>

    <body>

    <%

    Dim ateam(3),i

    ateam(0) = “Hannibal”

    ateam(1) = “Murdock”

    ateam(2) = “Faceman”

    ateam(3) = “B.A. Baracus”

    For i = 0 to 3

    response.write(ateam(i) & “<br />”)

    Next

    %>

    This program loops through all the values in the array and prints them to the monitor (don't worry if the code looks a little weird to you; we will cover loops soon). The result is:

      Hannibal

      Murdock

      Faceman

      B.A. Baracus

    You will note that when we declared our variable we placed a “3” in the brackets. These brackets determine the amount of elements that are in the array (each piece of data in an array is an element). As you can see, there are actually four pieces of data, which does not correspond with the number of elements we initially declared. This is because ASP (and all languages really) start the first element position at 0, and not at 1.

    It is possible to create more dynamic arrays, where the number of elements in the array change throughout the course of the program. To do this, you simply leave off the number of elements in the array, like so:


    <html>

    <body>

    <%

    Dim ateam()

    ReDim ateam(1)

    ateam(0) = “Hannibal”

    ateam(1) = “Murdock”

    ReDim Preserve ateam(3),i

    ateam(2) = “Faceman”

    ateam(3) = “B.A. Baracus”

    For i = 0 to 3

    response.write(ateam(i) & “<br />”)

    Next

    %>

    This code creates an array named ateam. It then uses the ReDim keyword to recreate the array to hold two elements and stores some values in it. Next, we use ReDim on the array again, however this time we don't want our previous data to disappear, so we use the Preserve keyword, to keep the data in the array. We then assign two more variables and finally loop through the array, printing out its values. The result:

      Hannibal

      Murdock

      Faceman

      B.A. Baracus

    More ASP Code Articles
    More By James Payne


       · Thanks for stopping by. In this article we discuss the basics of ASP and how to...
       · ASP? - not ASP.NET, but classic ASP? This *must* be a repost of some article...
       · Believe it or not, its a bonafide ASP without the .Net article. Aimed mostly at the...
     

    ASP CODE ARTICLES

    - ASP Forms
    - ASP: The Beginning
    - Getting Remote Files With ASP Continued
    - Inbox and Outbox Manipulation in ASP
    - Relational DropDownList Using VB.NET
    - Ad Tracking URL Hits
    - Use ViewState to display one record per page...
    - Send Email using ASP.NET formatted in HTML
    - ASP File Explorer
    - ASP/XML Interview questions by Srivatsan Sri...
    - Various methods of setting Date values to a ...
    - Conditional DataGrid Item and using checkbox...
    - Fill .NET Listbox with SQL DataReader
    - Filling Dropdown box using Code-Behinds in C#
    - FLAMES code sample written in .NET What is F...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT