ASP.NET 2.0 Tips - Shared Functions

Jun04
Missing Image
By Ken Foubert

When developing a recent project for ASP.NET, there was a need to migrate a large number of generic functions that were created in PHP into the .net web project. For .net 1.0, you were able to add a code page that could be used for creating a bunch of functions. However, in .net 2.0, you still had the ability to add a code page, but it had to be a class. This meant placing all of our migrated functions as methods inside a class. To use a generic function, such as generateNewPassword(), you would have to first create a new object for the class and then call the method.

Example:
Dim objGenericFunctions as new GenericFunctions()

blnPasswordSent = generateNewPassword(firstName, lastName, email)

There are a couple of problems with this technique. First, since .net 2.0 encourages object-oriented programming, you could either create objGenericFunctions() once and pass the object as a parameter to other classes. But this requires extra code management, especially when there are classes that call other classes that might need to use some of those Generic Functions from objGenericFunctions. Or you could create a new objGenericfunctions whenever you need it, but this means creating a number of objects for the GenericFunctions class, which seems to be a waste of system resources.

In developing classes for PHP 5.0, you could declare a class function as static. Which allowed you to use that method without having to create a new object. After some googling, I learned that VB.net also has a similar declaration, but they call it shared. So, by declaring a class function as shared, there's no need to create a new object each time.

Example:
blnPasswordSent = GenericFunctions.generateNewPassword(firstName, lastName, email)

Here's a sample class with a shared function:
Imports Microsoft.VisualBasic
Imports System.Web

Public Class GenericFunctions

   Public Shared Function isDateInRange(ByVal dateStart As DateTime, ByVal dateEnd As DateTime, ByVal dateCompare As DateTime) As Boolean

        Dim blnInRange As Boolean = False

        Dim intStartDiff As Integer = 0

        Dim intEndDiff As Integer = 0

        intStartDiff = Date.Compare(dateStart, dateCompare)

        intEndDiff = Date.Compare(dateCompare, dateEnd)

        If intStartDiff <= 0 And intEndDiff <= 0 Then

            blnInRange = True

        End If

        Return blnInRange

    End Function

End Class

We can take this one step for further by adding a namespace declaration to the class. By using a namespace, we can now add Imports GenericFunctions to the top of a code page and access those methods without having to type GenericFunctions first.

Sample of Namespace declaration:
Imports Microsoft.VisualBasic
Imports System.Web

Namespace GenericFunctions

    Public Class GenericFunctions
        ' Place all the functions here...
    End Class

End Namespace
Example of using the Imports statement:
Default.aspx.vb - Code Behind
Imports GenericFunctions

Partial Class Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender as Object, ByVal e As System.EventArgs) Handles Me.Load

        'No longer need to start with GenericFunctions

        blnPasswordSent =generateNewPassword(firstName, lastName, email)

    End Sub

End Class

Dev

Back To Feed