Skip to Content
Beginning ASP.NET 3.5: In C# and VB
book

Beginning ASP.NET 3.5: In C# and VB

by Imar Spaanjaars
March 2008
Intermediate to advanced content levelIntermediate to advanced
766 pages
21h 15m
English
Wrox
Content preview from Beginning ASP.NET 3.5: In C# and VB

5.4. Organizing Code

When you start adding more than just a few pages to your web site, you're almost certain to end up with some code that you can reuse in multiple pages. For example, you may have some code that reads settings from the web.config file that you need in multiple files. Or you want to send an e-mail with user details from different pages. So you need to find a way to centralize your code. To accomplish this in an ASP.NET 3.5 web site, you can use functions and subroutines, discussed next. To make these functions and subroutines available to all the pages in your site, you need to create them in a special location, which is discussed afterward.

5.4.1. Methods: Functions and Subroutines

Functions and subroutines (subs) are very similar; both allow you to create a reusable block of code that you can call from other locations in your site. The difference between a function and a subroutine is that a function can return data while a sub doesn't. Together, functions and subroutines are referred to as methods. You'll see that term again in the final part of this chapter that deals with object orientation.

To make functions and subs more useful, they can be parameterized. That is, you can pass in additional information that can be used inside the function or subs. Functions and subs generally take the following format:

VB.NET

' Define a function
Public Function FunctionName[parameterList] As DataType

End Function

' Define a subroutine
Public Sub SubName ([parameterList] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning ASP.NET 4: in C# and VB

Beginning ASP.NET 4: in C# and VB

Imar Spaanjaars
Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Bill Evjen, Scott Hanselman, Devin Rader

Publisher Resources

ISBN: 9780470187593Purchase book