For code that needs to be accessible throughout your application, App_Code is indeed the place to go. (Alternatively, if you own a copy of Visual Studio or have the Express edition of C# or VB
.NET, you can also move the code to a separate class library project and import that in your web project).
And yes, as the message suggests, you do need a class to put it in.
All (or actually, most) code needs a class to live in. But that's not too bad, is it?
You could, for example, create a Helpers class with Shared (static in C#) methods that are globally accessible. The advantage of a Shared method is that you don't need to new up an instance of the Helpers class. Something like this would work:
Public Class Helpers
Public Shared Function ReturnSomethingUseful() As String
Return "Something Useful"
In your aspx pages, you can then do something like this:
myLabel.Text = Helpers.ReturnSomethingUseful()
Is there something you don't like about this model? Do you have other ideas how you'd like this to behave?
If you don't use the Shared keyword, you need to create an instance of Helpers first:
Dim myHelpers As Helpers = new Helpers()
myLabel.Text = myHelpers.ReturnSomethingUseful()
Whether you can use Shared methods or not depends on the type of functionality in the method. With many helper methods, the Shared keyword works just fine.
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results
and Beginning Dreamweaver MX / MX 2004