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.
ANAT2403Asked:
Who is Participating?
 
praneethaCommented:
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
0
 
TimCotteeHead of Software ServicesCommented:
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
0
 
ANAT2403Author Commented:
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);
    }
0
 
enwhyseeCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.