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

Chapter 3: VB and C# Programming Basics

Visual Basic

Dim intX As Integer

Dim strY As String = "35" intX = Int32.Parse(strY) + 6

C#

int intX;

string strY = "35";

intX = Convert.ToInt32(strY) + 6;

Now, the computer will accept even with the C# code, because it ends up adding two numbers, rather than a number and a string, as we tried initially. This principle holds true whenever we’re mixing types in a single expression.

Arrays

Arrays are a special variety of variable that’s tailored for storing related items of the same data type. Any one item in an array can be accessed using the array’s name, followed by that item’s position in the array (its offset). Let’s create a sample page to see how it’s done. The results of this code are shown in Figure 3.3:

Visual Basic File: Arrays.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Arrays</title>

<script runat="server" language="VB"> Sub Page_Load()

' Declare an array

Dim drinkList(4) As String

'Place some items in it drinkList(0) = "Water" drinkList(1) = "Juice" drinkList(2) = "Soda" drinkList(3) = "Milk"

'Access an item in the array by its position drinkLabel.Text = drinkList(1)

End Sub </script>

</head>

<body>

<form runat="server">

<asp:Label id="drinkLabel" runat="server" />

62

Arrays

</form>

</body>

</html>

C#

File: Arrays.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Arrays</title>

<script runat="server" language="C#"> void Page_Load()

{

// Declare an array

string[] drinkList = new string[4];

//Place some items in it drinkList[0] = "Water"; drinkList[1] = "Juice"; drinkList[2] = "Soda"; drinkList[3] = "Milk";

//Access an item in the array by its position drinkLabel.Text = drinkList[1];

}

</script>

</head>

<body>

<form runat="server">

<asp:Label id="drinkLabel" runat="server" /> </form>

</body>

</html>

Figure 3.3. Reading an element from an array

There are some important points to pick up from this code. First, notice how we declare an array. In VB, it looks like a regular declaration for a string, except that

63

Chapter 3: VB and C# Programming Basics

the number of items we want the array to contain is provided in parentheses after the name:

Visual Basic

File: Arrays.aspx (excerpt)

Dim drinkList(4) As String

In C#, it’s a little different. First, we declare that drinkList is an array by following the data type with two empty square brackets. We then specify that this is an array of four items, using the new keyword:

C#

File: Arrays.aspx (excerpt)

string[] drinkList = new string[4];

A crucial point to realize here is that, in both C# and VB, these arrays are known as zero-based arrays. In a zero-based array, the first item has position 0, the second has position 1, and so on through to the last item, which has a position that’s one less than the size of the array (3, in this case). So, we specify each item in our array like this:

Visual Basic

File: Arrays.aspx (excerpt)

drinkList(0) = "Water" drinkList(1) = "Juice" drinkList(2) = "Soda" drinkList(3) = "Milk"

C#

File: Arrays.aspx (excerpt)

drinkList[0] = "Water"; drinkList[1] = "Juice"; drinkList[2] = "Soda"; drinkList[3] = "Milk";

Note that C# uses square brackets for arrays, while VB uses standard parentheses. We have to remember that arrays are zero-based when we set the label text to the second item, as shown here:

Visual Basic

File: Arrays.aspx (excerpt)

drinkLabel.Text = drinkList(1)

 

 

 

C#

File: Arrays.aspx (excerpt)

 

 

drinkLabel.Text = drinkList[1];

 

To help this fact sink in, you might like to try changing this code to show the third item in the list, instead of the second. Can you work out what change you’d need to make? That’s right—you need only to change the number in the brackets

64

Functions

to reflect the new item’s position in the array (don’t forget to start at zero). In fact, it’s this ability to select one item from a list using only its numerical location that makes arrays so useful in programming—we’ll experience this first-hand as we get further into the book.

Functions

Functions are exactly the same as subroutines, but for one key difference: they return a value. In VB, we declare a function using the Function keyword in place of Sub, while in C#, we simply have to specify the return type in place of void. The following code shows a simple example:

Visual Basic

File: Functions.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>ASP.NET Functions</title> <script runat="server" language="VB">

'Here's our function Function getName() As String

Return "Zak Ruvalcaba" End Function

'And now we'll use it in the Page_Load handler Sub Page_Load(s As Object, e As EventArgs)

messageLabel.Text = getName() End Sub

</script>

</head>

<body>

 

<form runat="server">

 

<asp:Label id="messageLabel" runat="server" />

 

</form>

 

</body>

</html>

C#

File: Functions.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>ASP.NET Functions</title> <script runat="server" language="C#">

// Here's our function

65

Chapter 3: VB and C# Programming Basics

string getName()

{

return "Zak Ruvalcaba";

}

// And now we'll use it in the Page_Load handler void Page_Load()

{

messageLabel.Text = getName();

}

</script>

</head>

<body>

<form runat="server">

<asp:Label id="messageLabel" runat="server" /> </form>

</body>

</html>

When the page above is loaded in the browser, the Load event will be raised which will cause the Page_Load event handler to be called, which in turn will call the getName function. Figure 3.4 shows the result in the browser.

Figure 3.4. Executing an ASP.NET function

Here’s what’s happening: the line in our Page_Load subroutine calls our function, which returns a simple string that we can assign to our label. In this simple example, we’re merely returning a fixed string, but the function could just as easily retrieve the name from a database (or somewhere else). The point is that, regardless of how the function gets its data, we call it in just the same way.

When we’re declaring our function, we must remember to specify the correct return type. Take a look at the following code:

Visual Basic

' Here's

our function

Function

addUp(x As Integer, y As Integer) As Integer

Return

x + y

 

 

66

Functions

End Function

' And now we use it in Page_Load

Sub Page_Load(s As Object, e As EventArgs) messageLabel.Text = addUp(5, 2).ToString()

End Sub

C#

//Here's our function int addUp(int x, int y)

{

return x + y;

}

//And now we use it in Page_Load void Page_Load()

{

messageLabel.Text = addUp(5, 2).ToString();

}

You can easily adapt the previous example to use this new code so that you can see the results in your browser—just replace the code inside the <script> tags in Functions.aspx with the code above.

The first thing to notice in comparing this new code to the original version of Functions.aspx is that our function now accepts parameters. Any function or subroutine can take any number of parameters, of any type (there’s no need for parameter types to match the return type—that’s just coincidental in this example).

We can readily use the parameters inside the function or subroutine just by using the names we gave them in the function declaration (here, we’ve chosen x and y, but we could have chosen any names).

The other difference between this and the function declaration we had before is that we now declare our function with a return type of Integer or int, rather than String, because we want it to return a whole number.

When we call the new function, we simply have to specify the required number of parameters, and remember that the function will return a value with the type we specify. In this case, we have to convert the integer value that the function returns to a string, so that we can assign it to the label.

The simplest way to convert an integer to a string is to append .ToString() to the end of the variable name. In this case, we appended ToString on the function

67