Build Your Own ASP.NET 3.5 Website Using C# & VB (16 page)

Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online

Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett

Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
11.2Mb size Format: txt, pdf, ePub

LearningASP\VB\Arrays.aspx
(excerpt)

drinkLabel.Text =
drinkList(1)

C#

LearningASP\CS\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 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 benefit first-hand as we get

further into the book.

Functions

Functions
are very similar to 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:

Licensed to [email protected]

62

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Visual Basic

LearningASP\VB\Functions.aspx

<%@ Page Language="VB" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




ASP.NET Functions










C#

LearningASP\CS\Functions.aspx
(excerpt)

<%@ Page Language="C#" %>




When the page above is loaded in the browser, the Load event will be raised, causing

the Page_Load event handler to be called; it, in turn, will call the getName function.

The getName
function returns a simple string that we can assign to our label. Fig-

ure 3.4 shows the result in the browser
.

Figure 3.4. Executing an ASP.NET function

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 some other location). 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 this code:

Visual Basic

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

Return x + y

End Function

Sub Page_Load(s As Object, e As EventArgs)

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

End Sub

C#

int addUp(int x, int y)

{

return x + y;

Licensed to [email protected]

64

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

}

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