Global subroutines

F

fd123456

Er... Sorry to interrupt, but you CAN put methods and functions in the
Global.asax file. You just have to make them static (Shared in VB).
You can even use functions normally not available outside of a page,
like LoadControl, like this :

in an aspx code-behind file, for instance in the Page_Load handler :

System.Web.UI.Page p = this;
bool IsLoaded = Global.GlobalMethod(ref p);


In the Global.asax file :

public static bool GlobalMethod(ref System.Web.UI.Page p)
{
p.LoadControl("mycontrol.ascx");
return (p != null);
}

(of course, you should use try/catch blocks and not check p against
null, but that was just for simplicity's sake).

So, yes, you can !

HTH,

Michel
 
S

Stefan Kiryazov (MCAD)

Of course, you can in a way... using Global is the same like defining
your own class.
 
T

tshad

fd123456 said:
Er... Sorry to interrupt, but you CAN put methods and functions in the
Global.asax file. You just have to make them static (Shared in VB).
You can even use functions normally not available outside of a page,
like LoadControl, like this :

in an aspx code-behind file, for instance in the Page_Load handler :

System.Web.UI.Page p = this;
bool IsLoaded = Global.GlobalMethod(ref p);


In the Global.asax file :

public static bool GlobalMethod(ref System.Web.UI.Page p)
{
p.LoadControl("mycontrol.ascx");
return (p != null);
}

I tried to put a nothing function in my Global.asax file, as you suggested
and get an error:

External component has thrown an exception.

My Global.asax looks like:

***************************************************
<script runat="Server">

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

</Script>
**********************************************************************

and the call in my page looks like:

*********************************************************************
Sub Page_Load(sender as Object, e as EventArgs)
dim temp as string
temp = Global.tom()

response.write("temp = ")
****************************************************************

Do I need to do something else?

Thanks,

Tom
 
T

tshad

tshad said:
I tried to put a nothing function in my Global.asax file, as you suggested
and get an error:

External component has thrown an exception.

Actually, that was the message I was getting from my error handler. When I
took out the handler and let asp.net send the error page, I get:

BC30451: Name 'Global' is not declared.

Do I need to define something, before I can use a shared routine?

Tom
 
T

tshad

Ok, I changed my code to look like this in trying to get a general Global
routine I can call from anywhere in my Application:

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
 
M

Mark Rae

Er... Sorry to interrupt, but you CAN put methods and functions in the
Global.asax file. You just have to make them static (Shared in VB).

Well obviously! Global.asax is just a class module like any other...
 
T

tshad

Mark Rae said:
Well obviously! Global.asax is just a class module like any other...

except I can't seem to get my Global function to work using Global.asax, but
then I didn't define it as a class.

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top