call which will return an integer during execution. Converting numbers to strings
is a very common task in ASP.NET, so it’s 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
messageLabel.Text = addUp(5, 2).ToString();
messageLabel.Text = Convert.ToString(addUp(5, 2));
Don’t be concerned if you’re a little confused by how these conversions work,
though—the syntax will become clear once we discuss object oriented concepts
later in this chapter.
Throwing around values with variables and functions isn’t 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 it’s applied to a value.
Don’t worry—operators 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 you’ll use most often in your ASP.NET development.
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.
Chapter 3: VB and C# Programming Basics
Table 3.2. Common ASP.NET operators
greater than or equal to>=>=
less than or equal to<=<=
not equal to!=<>
assigns a value to a variable==
create object or arraynewNew
The following code uses some of these operators:
If (user = "Zak" AndAlso itemsBought <> 0) Then
messageLabel.Text = "Hello Zak! Do you want to proceed to " & _
if (user == "Zak" && itemsBought != 0)
messageLabel.Text = "Hello Zak! Do you want to proceed to " +
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 they’re equal. Don’t