Home

Introduction to Conditions

 

Conditional and Boolean Values

 

Introduction to Conditional Values

A conditional control is a web control that allows a visitor to make a selection. There are various options. That is, there are different types of controls that allow a user to make different types of selections.

 

Boolean Values

A Boolean value is one that qualifies as being true or false. A variable is referred to as Boolean if it can hold a value that is either true or false.

To declare a Boolean variable, you can use the bool keyword. Here is an example:

bool drinkingUnderAge;

After declaring a Boolean variable, you can initialize it with true or false to make sure you know its initial value. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Active Exercise</title>
</head>
<body>

<h3>Active Exercise</h3>

<p> 
<%
    bool drinkingUnderAge = true;

    Response.Write("Drinking Under Age: " + drinkingUnderAge);
%>
</p>

</body>
</html>

At any time and when you judge it necessary, you can change the value of the Boolean variable by assigning it a true or false value.

Boolean Constants

The C# language provides three Boolean constants:

  • true: A value is true if it has meaning that can be evaluated to number, either positive or negative
  • false: A value is false if it can be evaluated to nil
  • null: An object is considered null if it doesn't hold a meaningful value. This constant is usually used on variables declared from classes

You should be familiar with the check box, the radio button, and the radio button list controls.

 
 
 
 

Enumerations

 

Introduction

An enumeration is a series of constant integers that each has a specific position in the list and can be recognized by a meaningful name.

Creating an Enumeration

To create an enumeration, you use the enum keyword, followed by the name of the enumeration, followed by a name for each item of the list. The name of the enumeration and the name of each item of the list follows the rules we reviewed for names. The formula of creating an enumeration is:

enum Series_Name {Item1, Item2, Item_n};

Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
    enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
</script>

<title>Active Exercise</title>
</head>
<body>

<h3>Active Exercise</h3>

<p></p>

</body>
</html>

After creating an enumerator, each item in the list is referred to as a member of the enumerator. To access a member of an enumeration, type the name of the enumeration, followed by a period, followed by the desired member. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
  enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
</script>

<title>Active Exercise</title>
</head>
<body>

<h3>Active Exercise</h3>

<%
    Response.Write("<pre>Member Index: ");
    Response.Write(HouseType.Unknown);
    Response.Write("</pre><pre>Member Index: ");
    Response.Write(HouseType.SingleFamily);
    Response.Write("</pre><pre>Member Index: ");
    Response.Write(HouseType.TownHouse);
    Response.Write("</pre><pre>Member Index: ");
    Response.Write(HouseType.Condominium);
%>

</body>
</html>

This would produce:

Real Estate

Managing an Enumeration

Each member is assigned a constant number. The members are counted starting at 0, then 1, etc. By default, the first member in the enumerationr is assigned the number 0, the second is 1, etc. This is referred to as the index. In our HouseType enumeration, the Unknown member has an index 0. The SingleFamily member has an index of 1. The TownHouse member has an index of 2.

You can specify or change the numbers to your liking when you create the enumeration but once the enumeration has been created, whether you specified these numbers or not, they cannot be changed.

To make the list start at a specific number, assign the starting value to the first item in the list. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
enum HouseType { Unknown = 5, SingleFamily, TownHouse, Condominium }
</script>

<title>Active Exercise</title>
</head>
<body>

<h3>Active Exercise</h3>

<%
    Response.Write("<pre>Member Index: ");
    Response.Write(HouseType.Unknown);
    Response.Write("</pre><pre>Member Index: ");
    Response.Write(HouseType.SingleFamily);
    Response.Write("</pre><pre>Member Index: ");
    Response.Write(HouseType.TownHouse);
    Response.Write("</pre><pre>Member Index: ");
    Response.Write(HouseType.Condominium);
%>

</body>
</html>

Enumerations are regularly used when performing comparisons.

 
 
   
 

Home Copyright 2009 FunctionX Next