ANAT2403
asked on
Where to keep gloabal function
I am working in VS2005 ASP.NET 2.0 Sqlserver 2000.
I have in my project some functions that I use in every page like show error message.
Where should I keey them?
I thought of creating a .cs file in the App_Code folder and I pit there a public function but when I calles it from the codebehind of a page it was not recognized.
I have in my project some functions that I use in every page like show error message.
Where should I keey them?
I thought of creating a .cs file in the App_Code folder and I pit there a public function but when I calles it from the codebehind of a page it was not recognized.
ASKER
What you wrote is good for visual basic. I am working in C#.
there is no "shared" but what I define in App_code is public. But from what I read I have to
define it as a class .
In the msdn: " Using Shared Code in Web Sites in Visual Web Developer "
so it doesn't work for me. first I tried to write ther a function but I coudn't call it and I don't know how to define it
as a class. This is my function:
public void ShowMessage(string keymsg,Page page)
{
string msg = (string)GetGlobalResourceO bject("Mes sages", keymsg);
string myScript = @"alert('" + msg + "')";
page.ClientScript.Register StartupScr ipt(this.G etType(), "MyScript", myScript, true);
}
there is no "shared" but what I define in App_code is public. But from what I read I have to
define it as a class .
In the msdn: " Using Shared Code in Web Sites in Visual Web Developer "
so it doesn't work for me. first I tried to write ther a function but I coudn't call it and I don't know how to define it
as a class. This is my function:
public void ShowMessage(string keymsg,Page page)
{
string msg = (string)GetGlobalResourceO
string myScript = @"alert('" + msg + "')";
page.ClientScript.Register
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yep, static would work in C# as praneetha mentions. So you could make a class that is called Globals, and then define the method as follows:
public class Globals
{
public static void ShowMessage(string keymsg,Page page)
{
string msg = (string)HttpContext.GetGlo balResourc eObject("M essages", keymsg);
string myScript = @"alert('" + msg + "')";
page.ClientScript.Register StartupScr ipt(this.G etType(), "MyScript", myScript, true);
}
}
Then, elsewhere, you could say Globals.ShowMessage("blah" , myPage); and it will work.
public class Globals
{
public static void ShowMessage(string keymsg,Page page)
{
string msg = (string)HttpContext.GetGlo
string myScript = @"alert('" + msg + "')";
page.ClientScript.Register
}
}
Then, elsewhere, you could say Globals.ShowMessage("blah"
You should be able to create a .cs file there called lets say "Globals" and then create your functions as shared:
Public Shared Function MyError(ByVal TheMessage As String)
Then call it as
Globals.MyError ("Message")
The key part is to make it a shared function, that way you do not need to create an instance of the class.
Tim Cottee