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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 10
    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

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek