Global subroutines

  • Thread starter Thread starter fd123456
  • Start date Start date
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
 
Of course, you can in a way... using Global is the same like defining
your own class.
 
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
 
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
 
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
 
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...
 
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
 
Back
Top