Public Functions in Global.asax?

M

Mat

Hi,

I am trying to create a set of global functions that are available to
any page. However I am not really getting anywhere, even after looking
at several examples.

This is the code I have so far:

(simple.aspx)
<%@ Page Language="vb" AutoEventWireup="false"
Inherits="RealSimple.CodeBehindSimple" src="simple.vb" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title></title>
</head>

<body>

<form runat="server">

<asp:Label id="moog" runat="server" />
<asp:Button id="btn_doit" runat="server" OnClick="buttonclick" />

</form>

</body>
</html>



(simple.vb)
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Namespace RealSimple
Public Class CodeBehindSimple
Inherits Page

Protected WithEvents moog As label

Public Sub New()
End Sub


sub buttonclick(s as object, e as eventargs)
moog.text = myGlobal.SayGoodbye("M")
end sub
End Class
End Namespace



(global.asax)
<%@ Application language="VB" Classname="myGlobal" %>
<script runat="server">
Sub Application_Start(Sender As Object, E As EventArgs)
' Code that runs on application startup
End Sub


Sub Application_End(Sender As Object, E As EventArgs)
' Code that runs on application shutdown
End Sub


Sub Application_Error(Sender As Object, E As EventArgs)
' Code that runs when an unhandled error occurs
End Sub


Sub Session_Start(Sender As Object, E As EventArgs)
' Code that runs when a new session is started
End Sub


Sub Session_End(Sender As Object, E As EventArgs)
' Code that runs when a session ends
End Sub


Public Shared Function SayGoodbye( name As String ) As String
Return name
End Function

</script>


The problem is, when try and fire this off (simple.aspx) I get the
following error:


BC30451: Name 'myGlobal' is not declared.


Any ideas what is going wrong?

I have looked at 'http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=708199'
which is what I have based my code on.

I am not using VS, just a simple text editor.

Many thanks,
Mat.
 
K

Kevin Spencer

Create a Class Library, and use them out of the Class Library.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
M

Marina

I think that is because your class is in one namespace, and myGlobal ends up
being in another.

In any case, I don't recommend you put any function in global other then the
ones it itself will need. Create a brand new class, and put your shared
functions in there.
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top