Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 30th, 2006, 04:50 PM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default App wide code - where to put it?

I'm writing an asp.net 2 site with many aspx pages. There will be functions and subs that will be employed by most of the pages. So, I want to put those functions where they
can be called from any page's code.

I have tried to put the functions in global.asax, but have been unable to access that file programmatically. After reading about it, I am not sure if that is even the right place to put common code. It seems to want only application event code.

Secondly, I tried to put the common functions in the app_code folder but when I try to do so, it prompts me for a new class or text file. I don't need a class to hold just one function.

Anyone help me out on finding a place to put app wide functions and subs? There's got to be a place for that.

Thanks.

VictorVictor


 
Old May 30th, 2006, 05:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi VictorVictor,

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"
  End Function
End Class

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.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old May 30th, 2006, 08:06 PM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar.
No, I don't have a problem with classes -- been using them since they came out. It just did not seem reasonable to me that I would have to create a class just to encapsulate a simple funtion. Guess not.
VV


 
Old May 31st, 2006, 01:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In .NET code has to live somewhere; you can't just place code somewhere and then hope it's accessible to the outside world.

As an alternative, you can take a look at Modules:

Public Module Helpers

End Module

All methods in a module are considered shared (so you can't add the Shared keyword to their declaration). However, personally I don't like them very much. They break the concepts of OO, and they make it very hard to upgrade your module to a true class later in the development cycle. As far as I know, there's not much (if anything, other than methods in a module having global scope without specifying the module name) that a module can do that a class with shared methods can't, so I always uses classes.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Emerald by The Tea Party (Track 10 from the album: Transmission) What's This?
 
Old May 31st, 2006, 02:13 PM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar. Roger that!

As it turned out, I made a CPageUtility class to hold a function that makes a modification to the code inherited from the master page, but unique to every page. I've subsequently made a couple of other functions that also apply to more than one page. They went in the class too. So, making a class turned out to be a good idea.

On modules and handlers: I have read the Beginning ASP.NET 2.0 book. There was no mention of modules and handlers there. (They can't cover everything since it's a beginning book.) Fortunately, I also have Evjen's Professional ASP.NET 2.0 book, where they are covered.

Thanks for the additional help.

VV

 
Old May 31st, 2006, 02:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Don't Let Me Be Misunderstood by Kill Bill OMPST- Santa Esmeralda (Track 10 from the album: Kill Bill (Volume One)) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is promised VB app code for Ch14? PatFrank BOOK: Beginning ASP.NET 2.0 and Databases 1 March 1st, 2007 08:33 PM
Php code to put&get images from mysql not working! angelbeans PHP Databases 1 September 8th, 2006 12:44 PM
Processing all pages, app or session wide VictorVictor ASP.NET 2.0 Basics 3 July 22nd, 2006 05:19 AM
App Code store in DLL Aal Pro VB 6 2 December 23rd, 2004 11:00 AM
Where do i put the code? bph Access 7 February 19th, 2004 02:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.