views:

330

answers:

1

I am looking for differences between those 3 ways of using static class in asp.net application scope. Will all of these point to the same class? Which one is preferable >object< defined declaratively inside global.asax or static class ?

examples:

<object runat="server" scope="application" class="classname" ID="objID"></object>

VS

public static class classname {}

VS

Application("a") = new classname();
A: 

I'm assuming that your edit means:

In global.asax.cs:

Application["a"] = new classname();

In classname.cs

public static class classname {}

In which case, they're much the same with the big exception that (classname)Application["a"] will refer to an instance of the classname class, whereas in the second example any calls to classname.MethodNameGoesHere() will be calling static methods of the classname class.

I'd go for static methods of a static class, rather than methods of an instance stored in Application, although I'd then think very carefully about what the methods in classname do, as they could be being called simultaneously as many people could be accessing the site at once.

Rob
Yes, Application["a"] would be least favorable. How about using <object> in global.asax. How is this different than static class?
newbe
It's functionally identical *I think* to using Application["a"], so I'd be not in favour of it - it's not clear, and not a particularly well known way of doing it, either! :)
Rob
As far as I know using Application["a"] is least favorable, because your accessing one big hash table instead of direct static object. This is a left over from asp days.
newbe
So question still remains, <object> in global.asax or static class. Which one is best and why?
newbe
@newbe - As markup in global.asax is functionally equivalent to Application["a"] = new classname(), as I said in my 1st comment, I'd not be in favour of it. Therefore, I'd go for a static class. That's personal preference. It also *seems* more correct.
Rob
I am not sure mark up from global.asax is functionally equivalent to Application["a"]. If this was true, anything declared with <object> tag would be added to Application hash table. I'll confirm this later and let you know.
newbe