call which will return an integer during execution. Converting numbers to strings
is a very common task in ASP.NET, so its good to get a handle on it early.
Converting Numbers to Strings
There are more ways to convert numbers to strings in .NET, as the following
lines of VB code illustrate:
messageLabel.Text = addUp(5, 2).ToString()
messageLabel.Text = Convert.ToString(addUp(5, 2))
If you prefer C#, these lines of code perform the same operations as the VB
code above:
messageLabel.Text = addUp(5, 2).ToString();
messageLabel.Text = Convert.ToString(addUp(5, 2));
Dont be concerned if youre a little confused by how these conversions work,
thoughthe syntax will become clear once we discuss object oriented concepts
later in this chapter.
Operators
Throwing around values with variables and functions isnt of much use unless
you can use them in some meaningful way, and to do so, we need operators. An
operator is a symbol that has a certain meaning when its applied to a value.
Dont worryoperators are nowhere near as scary as they sound! In fact, in the
last example, where our function added two numbers, we were using an operator:
the addition operator, or + symbol. Most of the other operators are just as well
known, although there are one or two that will probably be new to you. Table 3.2
outlines the operators that youll use most often in your ASP.NET development.
Operators Abound!
The list of operators in Table 3.2 is far from complete. You can find detailed
(though poorly written) lists of the differences between VB and C# operators
on the Code Project web site.
3
3
http://www.codeproject.com/dotnet/vbnet_c__difference.asp
68
Chapter 3: VB and C# Programming Basics
Table 3.2. Common ASP.NET operators
DescriptionC#VB
greater than>>
greater than or equal to>=>=
less than<<
less than or equal to<=<=
not equal to!=<>
equals===
assigns a value to a variable==
or||OrElse
and&&AndAlso
concatenate strings+&
create object or arraynewNew
multiply**
divide//
add++
subtract--
The following code uses some of these operators:
Visual Basic
If (user = "Zak" AndAlso itemsBought <> 0) Then
messageLabel.Text = "Hello Zak! Do you want to proceed to " & _
"checkout?"
End If
C#
if (user == "Zak" && itemsBought != 0)
{
messageLabel.Text = "Hello Zak! Do you want to proceed to " +
"checkout?";
}
Here, we use the equality, inequality (not equal to), and logical and operators
in an If statement to print a tailored message for a given user when he has put
a product in his electronic shopping cart. Of particular note is the C# equality
operator, ==, which is used to compare two values to see if theyre equal. Dont
69
Operators

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.