03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

178 x CHAPTER 5 PROGRAMMING YOUR <strong>ASP</strong>.<strong>NET</strong> WEB PAGES<br />

<strong>VB</strong>.<strong>NET</strong><br />

' Def<strong>in</strong>e a function<br />

Public Function FunctionName ([parameterList]) As DataType<br />

End Function<br />

' Def<strong>in</strong>e a subrout<strong>in</strong>e<br />

Public Sub SubName ([parameterList])<br />

End Sub<br />

C#<br />

// Def<strong>in</strong>e a function<br />

public DataType FunctionName([parameterList])<br />

{<br />

}<br />

// Def<strong>in</strong>e a subrout<strong>in</strong>e<br />

public void SubName([parameterList])<br />

{<br />

}<br />

The complete first l<strong>in</strong>e, start<strong>in</strong>g with Public, is referred to as the method signature because it<br />

def<strong>in</strong>es the look of the function, <strong>in</strong>clud<strong>in</strong>g its name <strong>and</strong> its parameters. The Public keyword (public<br />

<strong>in</strong> C#) is called an access modifier <strong>and</strong> def<strong>in</strong>es to what extent other web pages or code files can<br />

see this method. This is discussed <strong>in</strong> detail later <strong>in</strong> the chapter. For now, you should realize that<br />

Public has the greatest visibility, so the method is visible to any call<strong>in</strong>g code.<br />

The name of the function is followed by parentheses, which <strong>in</strong> turn can conta<strong>in</strong> an optional parameter<br />

list. The italic parts <strong>in</strong> these code examples will be replaced with real values <strong>in</strong> your code. The<br />

parts between the square brackets ([]) are optional. To make it a little more concrete, here are some<br />

examples of functions <strong>and</strong> subs:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Public Function Add(a As Integer, b As Integer) As Integer<br />

Return a + b<br />

End Function<br />

Public Sub SendEmailMessage(emailAddress As Str<strong>in</strong>g)<br />

' Code to send an e-mail goes here<br />

End Sub<br />

C#<br />

public <strong>in</strong>t Add(<strong>in</strong>t a, <strong>in</strong>t b)<br />

{<br />

return a + b;<br />

}<br />

public void SendEmailMessage(str<strong>in</strong>g emailAddress)<br />

{

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!