Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng].pdf
Скачиваний:
74
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 3: VB and C# Programming Basics

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 code above:

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.

Operators

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.

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

Operators

Table 3.2. Common ASP.NET operators

VB

C#

Description

>

>

greater than

>=

>=

greater than or equal to

<

<

less than

<=

<=

less than or equal to

<>

!=

not equal to

==

=

equals

=

=

assigns a value to a variable

OrElse

||

or

AndAlso

&&

and

&

+

concatenate strings

New

new

create object or array

*

*

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 they’re equal. Don’t

69