Setting up Shared routines in Global.asax

T

tshad

I am trying to set up some shared functions in my Global.asax file and can't
seem to get it to work.

In my Global.asax:

Public Class myUtils
inherits System.Web.HttpApplication

public shared function tom() as string
tom = "This is the shared sub"
end function
end class

In my page I do:

dim temp as string
temp = myUtils.tom()
response.write("temp = ")

This gives me an error:

Compiler Error Message: BC30451: Name 'myUtils' is not declared.
Source Error:
Line 34: Sub Page_Load(sender as Object, e as EventArgs)
Line 35: dim temp as string
Line 36: temp = myUtils.tom()
Line 37:
Line 38: response.write("temp = ")

Did I not define this correctly?

Do I need to import something? I was putting it into the Global.asax so I
wouldn't have to.

Thanks,

Tom
 
K

Karl Seguin

One can only guess that myUtils and your page aren't in the same namespace.
This being the case they must either be placed in the same namespace or you
must import the myUtils namespace. One possibility is that if you changed
the class of your Global.asax from Global to myUtils, the global.asax's
inherits property (at the top) is still pointing to the Global class, hence
myUtils isn't being compiled.

If you are using VS.Net, check out the "class" view to see where myUtils and
your page are with respect to each other.

Also, i'm not sure about the idea of putting this shared functionality in
your global.asax. Typically I'd create a separate class for this type of
shared functionality, which doesn't need to inherit from HttpApplication.
In my mind if you add a function to a class which inherits something, you
are basically extending this behaviour. Does your function actually
extends the behaviour of HttpApplication? It'll work either way though
....just my thoughts...

Karl
 
T

tshad

Karl Seguin said:
One can only guess that myUtils and your page aren't in the same
namespace.
This being the case they must either be placed in the same namespace or
you
must import the myUtils namespace. One possibility is that if you changed
the class of your Global.asax from Global to myUtils, the global.asax's
inherits property (at the top) is still pointing to the Global class,
hence
myUtils isn't being compiled.

I am trying to set this up as a shared file, so I don't have to import
classes. All I want to do is set up one function that all my pages can use.
This function just opens my database connection and checks to make sure the
connection is made (only 3 lines long). I don't really want to set up a
separate class and .dll for it. I was under the impression that I could do
this using the Global.asax file if I set it up as shared.
If you are using VS.Net, check out the "class" view to see where myUtils
and
your page are with respect to each other.

I am only using Dreamweaver.
Also, i'm not sure about the idea of putting this shared functionality in
your global.asax. Typically I'd create a separate class for this type of
shared functionality, which doesn't need to inherit from HttpApplication.
In my mind if you add a function to a class which inherits something, you
are basically extending this behaviour. Does your function actually
extends the behaviour of HttpApplication? It'll work either way though
...just my thoughts...

I don't know if it extends the HttpApplication behavior or not, I just used
this from an example I saw.

Thanks,

Tom
 

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

Top