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

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
 
D

dbui

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..."
 
D

dbui

I think what you meant as followed:

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

I tried that and got an error : "Server throws error exception." .

thanks

Tampa .NET Koder said:
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
 
J

John Saunders

dbui said:
I have the following code in global.asax.vb

Public UO As UNIOBJECTSLib()

First, never use public fields.

Private _UO As New 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()

You don't need the above, since I used New in the declaration.
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?

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
 
K

Kyril Magnos

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
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top