Where to keep gloabal function

Posted on 2006-04-11
Last Modified: 2008-02-26
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.
Question by:ANAT2403
    LVL 44

    Expert Comment

    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

    Author Comment

    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);
    LVL 15

    Accepted Solution

    shared is equivalent to static in C# if i am nto wrong

    so once u declare the mehtod as static you can call ut using Globals.ShowMEssage.

    good luck
    LVL 6

    Expert Comment

    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.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now