Global subroutines

F

fd123456

Hi tshad, sorry for not coming back to you earlier.

When I meant to add a method to Global.asax, I actually meant to add
it to the code-behind file. If you click on "Global.asax" in the file
explorer, then on the F7 key, you can edit this code-behind; that's
the one that includes the Application_Start, Application_BeginRequest,
etc, routines (I don't even know where you added the <script> bit).

In this file, you can add your own methods and they should be callable
from anywhere in your code, provided they are Public and Shared.

Your routine in Global should be :

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

(although I believe that your version was correct, but I'm not too
good with VB syntax)

and in your test aspx file, it should be :

Sub Page_Load(sender as Object, e as EventArgs)
Response.Write(Global.tom())
End Sub

(actually, once again, I'm not comfortable with VB and it is very
possible that you should omit the "()" after "tom").

Regarding your "MyUtils" attempt, I believe that this helper class
should not inherit from HttpApplication. My guess is that : 1) you
would have two concurrent applications running, not talking to each
other, provided that 2) you had Run that second application, which you
have not. Just don't inherit it from anything, put it in it's own
file, make sure that the NameSpace is the same as the rest of your app
(which should already be the case as you have added the class in your
Global.asax code-behind file) and you should be all set. Oh, and also
check that your MyUtils class is at the same level than the Global()
class, not inside, because then you would have nested classes, which
you would have to call through Global().MyUtils.MyMethod, and the
scope rules are a bit different then.

But once again, you don't *need* to create a class, you can just add
the method to the existing Global ones.

Tell me if you still run into trouble.

Michel
 
T

tshad

fd123456 said:
Hi tshad, sorry for not coming back to you earlier.

When I meant to add a method to Global.asax, I actually meant to add
it to the code-behind file. If you click on "Global.asax" in the file
explorer, then on the F7 key, you can edit this code-behind; that's
the one that includes the Application_Start, Application_BeginRequest,
etc, routines (I don't even know where you added the <script> bit).

I don't have a code-behind file and I am not using VS, unless you are
calling the Global.asax a code behind file (which I guess it is).
In this file, you can add your own methods and they should be callable
from anywhere in your code, provided they are Public and Shared.

It is still not working.

I was hoping I didn't need to create a class for just a small routine
(actually this routine is only going to open an SQL connection and check to
make sure it actually opens)

Here is my Global.asax file:

<script runat="Server">
public shared function tom() as string
tom = "This is the shared sub"
end function
</Script>

Here is my test file
******************************************************************************
<%@ Page Language="VB" trace="true" debug="true" AutoEventWireup="true"
ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<html>
<script runat="server">

Sub Page_Load(sender as Object, e as EventArgs)

if not IsPostBack then
response.write(Global.tom())
end if

End Sub
</script>
<head>
<title>Skills Test</title>
</head>
<body>
</body>
</html>
********************************************
The error I get is:

Compiler Error Message: BC30451: Name 'Global' is not declared.

Do I need to declare something in my file to use it?

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top