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] ...
Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.