How to access an object variable in global.asax.vb from a page?

  • Thread starter Thread starter dbui
  • Start date Start date
D

dbui

I have the following code in global.asax.vb

Public UO As UNIOBJECTSLib()

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

UO = new UniObjectsLib()

End Sub

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

' Fires when the application is started

Application("MyObj") = UO

End Sub

How can I retrieve this UO obj from a page?

thanks
 
thanks Tam,

I did try that b4 posting the question and it didn't work. It gave me an
error, something like "Object instance was not set..."
 
Hi dbui,

I think that the problem is not how you are creating your object, but
rather that you are not checking to see if the object exists in the
application. That is probably why you are getting the "Object instance not
set..." exception.

Dim obj as New MyObject
obj = CType(Application("MyObj"), MyObject)

If(obj IsNot Nothing) Then
....
End If

--
HTH

Kyril

Try

Dim obj as New MyObject

obj = Ctype(Application("MyObj") , MyObject)

It is key to use the NEW opeator to create objects in memory, otherwise
you will get this error
 
Thanks John

John Saunders said:
First, never use public fields.

Private _UO As New UNIOBJECTSLib()


You don't need the above, since I used New in the declaration.


Public Property UO As UNIOBJECTSLib
Get
Return DirectCast(Application("MyObj"), UNIOBJECTSLib)
End Get
End Property

Then, from a page, you can use:

Dim g As Global = DirectCast(Context.ApplicationInstance, Global)
Dim LocalUO As UNIOBJECTSLib = g.UO
 

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

Back
Top