Chapter 5
This chapter covers the basics of functions and their implementation in ASP.NET using C#.
Exercise 1
Determine whether a function will return a value for each of the following scenarios and justify your choice:
Solution
Calculate the due date of a book being checked out of a library: The function should return a value because we will execute some code and return a value (the due date).
Find out on which day of the week (Monday, Tuesday, and so on) does a certain date fall in the future: The function should return a value because we will execute some code and return a value (the day of the week).
Display a string determined by the marketing department and stored in a text file in a label: The function should not return a value because we will only execute some code to perform an action.
Exercise 2
List where and when are values held when a variable is used as a parameter passed by value. Do the same for a variable for a variable that is a parameter passed by reference.
Solution
Passing a parameter by value holds the value in the original place and in the function. So while the function is running, two copies of the value exist of which one may be modified in the function. The copy in the function will be destroyed when the function finishes executing.
Passing a parameter by reference holds the value in only one place. Changes made to the value during the function will affect the sole copy and will be useable by both the function and the calling code. Although the function will stop using the value when it finishes executing, the value will persist in the calling code.
Exercise 3
Write a function that generates a set of random integers. Build an ASP.NET page that allows you to enter the lower and upper bounds, and generate a set of random numbers within that range.
Solution
The ASP.NET page could be written as follows (available in the code download as 57084_Ch05_Ans03.aspx :
<%@ Page Language="C#" Debug="True" %>
<script runat="server">
void Page_Load()
{
if (IsPostBack)
{
/* The easiest way to see if people have written numbers in the
textbox is to try and convert them to integers as required.
If an error occurs, they aren't. We use the try catch to write
them a note to use numbers. See Chapter 14 on handling errors for
more on the try catch statement */
try
{
int Bound1 = Convert.ToInt32(txtBound1.Text);
int Bound2 = Convert.ToInt32(txtBound2.Text);
int NumberOfNumbers = Convert.ToInt32(txtQty.Text);
/* Check first if the numbers are the same. If they are, tell the
user off. If not, find out which is larger and pass that as the
upper bound to RowOfNumbers. Pass the other number as the lower
bound. */
if (!(Bound1 == Bound2))
{
if (Bound1 < Bound2)
{
RowOfNumbers(Bound1, Bound2, NumberOfNumbers);
}
else
{
RowOfNumbers(Bound2, Bound1, NumberOfNumbers);
}
}
else
{
lblOut.Text = "<hr>The two numbers must be different!";
}
} // end of try statement
catch
{
lblOut.Text = "<hr>Please enter three numbers. No tricks now!";
}
} // End of if postback
CleanUI();
}
/*
Cleans up the input boxes after the random numbers have been displayed
*/
void CleanUI()
{
txtBound1.Text = ";
txtBound2.Text = ";
txtQty.Text = ";
}
/*
Generates a random number. Uses a seed value to make sure the number is
random each time.
*/
int GenerateRandomNumber(int LowerBound, int UpperBound, int seed)
{
Random objRandom = new Random(seed);
int RandomNumber = objRandom.Next(LowerBound, UpperBound);
return RandomNumber;
}
/* Generates the row of as many numbers as requested*/
void RowOfNumbers(int LowerBound, int UpperBound, int Quantity)
{
lblOut.Text = "<hr>" + Quantity.ToString() +
" random numbers between " + LowerBound.ToString();
lblOut.Text += " and " + UpperBound.ToString() + " inclusive: ";
for (int i=1; i<=Quantity; i++)
{
lblOut.Text += GenerateRandomNumber(LowerBound, UpperBound,
i).ToString();
lblOut.Text += " ";
}
}
</script>
<html>
<head>
<title>Random Number Generator</title>
</head>
<body>
<form runat="server">
Number 1:
<asp:TextBox id="txtBound1" runat="server"></asp:TextBox>
<br />
Number 2:
<asp:TextBox id="txtBound2" runat="server"></asp:TextBox>
<br />
Quantity of Numbers:
<asp:TextBox id="txtQty" runat="server"></asp:TextBox>
<br />
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
<br />
<asp:Label id="lblOut" runat="server"></asp:Label>
</form>
</body>
</html>