* NEWBIE * Global Objects and Variables in ASP.NET

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
 
S

Scott Allen

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.
 
G

Guest

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?
 
G

Guest

Oops! I have actually not created the object which it is trying to pass on!

All works fine now I think.

Many thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top