views:

47

answers:

1

I have a function in a class file called auth_user and its in App_code folder. I am trying to call that function from random pages that are on the website. Inside the class file is a function that is simple, basicly check for flags in the sessions, i just wanna have it there so i dont have to type it again and again. I want to be able to call it with one function like auth_user();

How would i do this excetly ? would the function be public static void or what ?

+1  A: 

Static makes sense for this:

public class AuthUtility
{
    public static bool IsUserAuthorized()
    {
      ....
      return retVal;
    }
{

And then you would call it:

AuthUtility.IsUserAuthorized();

Edit Based on Comments

So, not to be rude, but that information in your comments would've been pertinent in your original question and saved a fair amount of time.

Regardless, you'll need to pass in either the current HTTPContext or the Current Session into your static method:

public class AuthUtility
{
    public static void AuthorizeUser(HttpSessionState currentSession)
    {
        currentSession["whatev"] = "rockin";
        .....
    }
}

And the you would call it:

AuthUtility.AuthorizeUser(this.Session);
Jacob G
cant get it to work.. i get confused with the static, void and allt that.. dont get the whole diffrence..if the function is not returning any value.. i would use void right ?the function is just doing some stuff.. but not returning some value..
eski
If you're not returning a value, then void would be correct. Static means that you don't need to create a new instance of the class to call the method. What error are you getting?
Jacob G
This works if i use public void a_user().. then i have to make a new instance of it then run it, dont want that.if i put static in it i get some errors like this."Error 1 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'"I am using sessions and server.transfers in the function
eski
Answer edited based on comment.
Jacob G