Link to home
Start Free TrialLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

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.
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi ANAT2403,

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
Avatar of ANAT2403

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)GetGlobalResourceObject("Messages", keymsg);
        string myScript = @"alert('" + msg + "')";
        page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", myScript, true);
    }
ASKER CERTIFIED SOLUTION
Avatar of praneetha
praneetha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of enwhysee
enwhysee

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.GetGlobalResourceObject("Messages", keymsg);
        string myScript = @"alert('" + msg + "')";
        page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", myScript, true);
    }
}


Then, elsewhere, you could say  Globals.ShowMessage("blah", myPage); and it will work.