[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Where to keep gloabal function

Posted on 2006-04-11
4
Medium Priority
?
521 Views
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.
0
Comment
Question by:ANAT2403
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 16425132
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
 

Author Comment

by:ANAT2403
ID: 16425612
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
 
LVL 15

Accepted Solution

by:
praneetha earned 1500 total points
ID: 16430905
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
 
LVL 6

Expert Comment

by:enwhysee
ID: 16435066
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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