0

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

sarsnake
  • 26,667
  • 58
  • 180
  • 286
  • Check out Kevin's answer. If you can define and use extension methods on Page class (or in 2.0 consistently use your own base class instead of Page) this will give code syntax you are looking for. Controls or web services would require a bit of extra work though. – DK. Dec 14 '09 at 18:57
  • I actually create 2 Utils-like static classes: Util for stuff used only by the local project, and SharedUtil (or similar) for the shared common library - this avoids ambiguity errors. I also put the shared library in a separate source repository to better handle versioning. – devstuff Dec 14 '09 at 19:01
  • BTW, the local Util is normally marked as `internal`, and the SharedUtil is `public`. – devstuff Dec 14 '09 at 19:02

6 Answers6

5

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
  • 242,243
  • 40
  • 408
  • 536
  • +1. No need to instantiate a utils class, just go with statics. – Jerod Venema Dec 14 '09 at 18:34
  • 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 Dec 14 '09 at 18:36
3

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.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
2

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
  • 28,324
  • 9
  • 63
  • 88
  • can't just access HttpContext.Current.Session anywhere without passing references? seems messy – sarsnake Dec 14 '09 at 18:42
  • 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 Dec 14 '09 at 19:04
  • 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! – sarsnake Dec 14 '09 at 23:05
1

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
  • 6,403
  • 6
  • 30
  • 29
  • 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. – Rebecca Dec 14 '09 at 20:35
0

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

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
0

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.

Pete H.
  • 1,427
  • 1
  • 12
  • 16