ASP.NET Basics (part 2): Not My Type - A New Assignment
(Page 3 of 10 )
Now, you might not know this, but you've already seen the "string" data type in action. In the first part of this tutorial, I used the following script to introduce you to variables.
<script language="c#" runat="server">
void Page_Load()
{
string name; // define a variable as a string type
name = "Neo"; // put some initial value into it
whoami.Text = "I am " + name + ". Welcome to my world.";
}
</script>
<html>
<head><title>Who am I?</title></head>
<body>
<asp:label id="whoami" runat="server" />
</body>
</html>
In this example, the variable "name" has been defined as a string, and stores the string value "Neo". As you will see, a similar technique can be used to define other types of variables also.
There are two important operators in this example that I "conveniently" forgot to mention when I first introduced it. Now that my memory is fully restored, though, let me bring them to your attention. The first is the assignment operator, as illustrated in the snippet below:
<%
name = "Neo"; // put some initial value into it
%>
The assignment operator in C# scripts, like in most other languages, is the = symbol, and it's used to assign a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable,
<%
name = myname;
%>
or an expression,
<%
sum = (10 * 2) + (20 / 5);
%>
or even an expression involving other variables
<%
diff = temp - 20;
%>
The assignment operator will be a staple of your C# experience - nary a day will pass when you don't use it - so get used to seeing it around.
So that takes care of the first operator - now for the second:
<%
whoami.Text = "I am " + name + ". Welcome to my world.";
%>
You'll notice that this line of code combines three strings to form a single string. This is accomplished by using the concatenation (+) operator, whose primary job is to add strings together. You can also use it to add numbers - I'll show you that shortly.
Next: Strange Characters >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire