C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Demo for functions


Posted Date: 29 Nov 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: SravyaMember Level: Gold    
Rating: 1 out of 5Points: 10



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>



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Web form  .  Functions  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Array of Text Boxes
Previous Resource: UserControl Events using Delegates
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use