Introduction
6 Feb 2008 @ 02:37PM

Updated: 22 Jan 2010 @ 02:37PM
This tutorial will be going deeper into the logic available within C#. Logic is really what makes a programming language into a programming language. It's what lets the language make decisions based on various criteria. A scripting language like HTML lacks the ability to make any decisions based on various inputs (at least so far) and therefore a pure HTML page will always render the same for everyone. With C# you can customize your output based on any number of inputs.

The primary method of decision making is the if - then - else construct. There are then various extensions on this basic construct that make programming long or intricate statements easier. This tutorial builds off the examples used in the previous two tutorials. We'll be beginning with the following code:
Comments (0)

using System;

public partial class _Default : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
          string contentString = null;
          string name = Request.Form.Get("name");
          string birthdate = Request.Form.Get("birthdate");
          string occupation = Request.Form.Get("occupation");

          try
          {
               if (name.Length > 0)
               {
                    contentString += "Hello " + name + ", thanks for filling out the form.<br>";
               }
          }
          catch { }
          try
          {
               if (birthdate.Length > 0)
               {
                    contentString += "I see you were born " + birthdate + ".<br>";
               }
          }
          catch { }
          try
          {
               if (occupation.Length > 0)
               {
                    contentString += "How do you like being a " + occupation + "?<br>";
               }
          }
          catch { }

          contentString += @"<form method='POST' action='default.aspx'>
          <table>
          <tr>
               <td>Name</td>
               <td><input name='name' value='"
+ name + @"'></td>
          </tr><tr>
               <td>Birth Date</td>
               <td><input name='birthdate' value='"
+ birthdate + @"'></td>
          </tr><tr>
               <td>Occupation</td>
               <td><input name='occupation' value='"
+ occupation + @"'></td>
          </tr><tr>
               <td colspan=2><input type='submit' value='Submit'></td>
          </tr>
          </table>
          </form>"
;
          content.InnerHtml = contentString;
     }
}
Comments (0)
So let us begin by diving right into the if / then / else statement.
Comments (0)