ASP.NET
  Home arrow ASP.NET arrow Page 4 - ASP.NET Basics (Part 6): Fully Function-al
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 
Moblin 
JMSL Numerical Library 
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.NET

ASP.NET Basics (Part 6): Fully Function-al
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2003-11-11

    Table of Contents:
  • ASP.NET Basics (Part 6): Fully Function-al
  • The Right Spirit
  • Turning Up the Heat
  • Sweet Tooth
  • Passing the Buck
  • Going Nowhere
  • First Date
  • Flavor of the Month

  • 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.NET Basics (Part 6): Fully Function-al - Sweet Tooth


    (Page 4 of 8 )

    Of course, it's also possible to specify an explicit return value with the "return" statement, as in the next example


    <SCRIPT language=c# runat="server">

    // define and initialize variable    
    int time 9;

    // define a function
    string whatYouDoing() 
    {
        
        
    // check the condition
        
    if (time == 9
        {
            return 
    "On my way to work";
            
        } 
        else if (
    time == 6)
        {
            return 
    "Done with work, on my way to dinner with friends";
        }
        else 
        {
            return 
    "I'm sleeping";
        }
    }

    void Page_Load()
    {   
        
    answer.Text "Right now? " whatYouDoing();


    </SCRIPT>
     Hi, what are you doing right now?
    <asp:label id=answer runat="server"></asp:label>



    Notice how the function is able to read the values of variables defined outside the function. This is related to variable scope in C#, and I plan to discuss it in detail a little further down.

    Return values need not be numbers or strings alone - a function can just as easily return an array, as demonstrated in the following example:


    <SCRIPT language=c# runat="server">

    // define a function
    // to return the dessert 
    // menu as an array
    string [] DessertMenu() 
    {
        
    string [] desserts = {"chocolate mousse""tiramisu""apple pie""chocolate chip cookies""caramel custard""butter scotch icecream",
    "blackforest cake"};        
        return 
    desserts;
    }

    void Page_Load()
    {   
        
    // get the system time
        
    DateTime date System.DateTime.Now;

        
    // cast the day of the week
        // as an integer
        
    int day = (int) date.DayOfWeek;
        
        
    // define an array to
        // to store our dessert array
        
    string [] menu;
        
        
    // call the function
        
    menu DessertMenu();
        
        
    // write the day of the week…
        
    today.Text date.DayOfWeek.ToString();

        
    // ... and the dessert for the day
        
    special.Text menu[day];


    </SCRIPT>
     It's <asp:label id=today runat="server"></asp:label>today, and our chef's whipped up some great <asp:label id=special runat="server"></asp:label>for today's special. Would you like some? 



    This is what you should see in the browser.

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...





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