Following is the code-snippet demostrate functions:
<%@ Page Language="C#" Debug="true" %> <script runat="server">
void Page_Load() { if (IsPostBack) { // Assign the result of Disguise() to a variable named DisguisedWord string DisguisedWord = Disguise(txtIn1.Text); lblDisguised.Text = DisguisedWord; // Assign the result of JoinWithDash() to the property of an object lblJoinedText.Text = JoinWithDash(txtIn1.Text, txtIn2.Text); // Use the result of JoinWithDash() as the argument of Blank() lblJoinedAndBlanked.Text = Blank(JoinWithDash(txtIn1.Text, txtIn2.Text)); // Use the result of IsString1Longer() as the expression in a control structure if (IsString1Longer(txtIn1.Text, txtIn2.Text)) { lblCompareLengths.Text = "String one is longer than string two."; } else { lblCompareLengths.Text = "String one is shorter than or the same length as string two."; } } } // 'Disguises' a string by adding one to each characters ASCII value string Disguise(string String1) { string DisguisedString; Byte[] myBytes = System.Text.Encoding.ASCII.GetBytes(String1); for (int i=0;i<myBytes.Length;i++) { myBytes[i] += 1; } char[] myChars=System.Text.Encoding.ASCII.GetChars(myBytes); DisguisedString = new string(myChars); return DisguisedString; } // Returns a concatenation of two texts with a separating hyphen string JoinWithDash(string String1, string String2) { return String1 + " - " + String2; } // Returns the string, replacing all characters with asterisks string Blank(string String1) { string BlankString = ""; for (int i=1; i <= String1.Length; i++) { BlankString += "*"; } return BlankString; } // Checks if string1 is longer than string2. Returns true if this is so bool IsString1Longer(string String1, string String2) { return (String1.Length > String2.Length); }
</script> <html> <head> <title>Chapter 5 : Using Functions Which Return Values</title> </head> <body> <form runat="server"> <asp:TextBox id="txtIn1" runat="server"></asp:TextBox> <br /> <asp:TextBox id="txtIn2" runat="server"></asp:TextBox> <br /> <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button> <br /> Function used in a variable: <asp:Label id="lblDisguised" runat="server"></asp:Label> <br /> Function used as value for an object property: <asp:Label id="lblJoinedText" runat="server"></asp:Label> <br /> Function used as an argument in another function: <asp:Label id="lblJoinedAndBlanked" runat="server"></asp:Label> <br /> Function used as an expression: <asp:Label id="lblCompareLengths" runat="server"></asp:Label> </form> </body> </html>
|
No responses found. Be the first to respond and make money from revenue sharing program.
|