views:

80

answers:

6

Never seen this done in asp.net, but never the less, can I define functions without being part of the class?

What I would like to have is a utility library. Currently I have Utils class and every time I need to use it for things like populating drop down lists i have to create and init the Utils() object...any way around that hassle aside from declaring the class static which I would rather not do as I access session in it?

I am using c#, not VB.

Thanks

+3  A: 

You can always use Extension Methods.

http://msdn.microsoft.com/en-us/library/bb383977.aspx

You can then add the methods on to the existing objects.

You could also create a base class which all your pages inherit from and have that contain the methods you need. It's still part of a class, but you don't need to instantiate a new one, or use static methods.

Kevin
...even extension methods belong to a class.
Justin Niessner
oh absolutely. You can't get around having classes, but I was trying to offer an alternative to the problem he was having.
Kevin
+5  A: 

There's no way to have methods outside of classes.

The typical solution in your case is to create a Utility class full of static methods...that way you don't have to worry about creating an instance of the class to utilize its methods.

And like Joel mentioned...you can still access the session from a static method.

Justin Niessner
+1. No need to instantiate a utils class, just go with statics.
jvenema
VB can have modules. These compile into a default class with a private constructor and the methods become shared. While the MSIL is still a class, the code looks like a method without a containing class.
Jarrett Meyer
A: 

If the class has state then leave it alone. Alternatively, take your state as parameters and then make it static.

ChaosPandion
+1  A: 

You could have all you pages derive from a BasePage class and put all of your util methods (or wrappers to them) into the base page class

feihtthief
I do this too, but it means that you have to have some code duplication. I.e. PageBase that inherits from System.Web.UI.Page and ControlBase that inherits from System.Web.UI.UserControl.Everything else goes in a Utils static class.
Junto
A: 

You can access the Session variables using HttpContext.Current.Session[], and you can do this from any class (In fact, in many applications I have that use Session variables, I encapsulate all of my session variables in their own class).

Having said that, there is no way to have a method outside of a class, and there really isn't a [good] reason to do so.

Gus
+2  A: 

You can still access Session variables in a static class. One way might be like this:

public static class Utils
{
    private static HttpSessionState Session
    {
        get { return HttpContext.Current.Session; }
    }

    public static string DoThing(string input)
    {
        // here you can access session variables like you're used to:
        Session["foo"] = input;
    }
}
Joel Mueller
can't just access HttpContext.Current.Session anywhere without passing references? seems messy
gnomixa
Yes, you can access `HttpContext.Current.Session` anywhere. I just threw in the static property as an example of how you can avoid changing existing code, or writing out "HttpContext.Current.Session" multiple times - that's what seems messy to me.
Joel Mueller
I actually totally forgot that I ahve already encapsulated all session access in one class called httphelper, which is static. I did it awhile back and it totally slipped my mind. So now every time I need to access session all I have to do is HttpHelper.Session(key).So I guess my problem has eliminated itself.Thanks!
gnomixa