* NEWBIE * Global Objects and Variables in ASP.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi There,

I have a ASP.NET application with several web forms and classes. How can I
make an object available to all web forms?

The object in question is used to access a database and fill a collection
object, so I only want to do this once and allow all web forms access to this
information.

Hope this makes sense!

Many thanks,

David Whitchurch-Bennett
 
You could add a static (shared in VB) method to a class that returns
the object.

Alternatively, you could just add the object to the Cache collection.
Then if noone is using the object it can be clean up by the runtime to
make room for other stuff.
 
Thanks for the quick response!

I have tried to do this, and here is the code: -

Public Class Globals

Public Shared PhotoDB As clsPhotoDB

End Class

and the code which references it...

cPhotoDB = Globals.PhotoDB
With cPhotoDB
.Get_Categories()
For Each sCategory In .Categories
sPhoto = New wcPhoto
With sPhoto
.Category = sCategory
pnlPhotos.Controls.Add(sPhoto)
End With
Next

End With

Now I get the following error: -

Object reference not set to an instance of an object.

I know how to create the object with the NEW keyword, but where or how would
I do this for a global object?
 
Oops! I have actually not created the object which it is trying to pass on!

All works fine now I think.

Many thanks.
 
Back
Top