static-classes

ASP.NET MVC, ActionFilters, static classes and passing data around...

I'd like to hear your opinions and maybe better suggestions for the following scenario: I have define a custom ActionFilter that does some job and comes out with some value. I would like to use that value in controller actions and in models. Now, I could use TempData to pass this value from the ActionFilter to any controller action met...

Wrapping session handling inside the static class

I separated all direct session interaction into a separate class and made it static, because I didn't want to create a new object several times. However, i wish to make sure that there are not concurrency issues or other wonky suprises. Here is the code: public static class HttpHelper { public static string Get(string key) { ...

Extending the Enumerable class in c#?

Hi, I have situation to extend the Enumerable class in c# to add the new Range method that accepts long parameters. I cannot define the method like this public static IEnumerable<long> Range(this Enumerable source, long start, long length) { for (long i = start; i < length; i++) { yield return i; } } Since extens...

Extension methods on a static class?

I know i can do the below to extend a class. I have a static class i would like to extend. How might i do it? I would like to write ClassName.MyFunc() static public class SomeName { static public int HelperFunction(this SomeClass v) ...

[C#] Odd Static Class Issue

Below is my very simple static class. Not sure what is wrong. I am using it in a non static class that has a correct "using" statement. Intellisense sees the class and its one method. I am getting the error The name 'SQLUserDataManager' does not exist in the current context". public static class SQLUserDataManager { ...

C# Static class vs struct for predefined strings

A co-worker just created the following construction in C# (the example code is simplified). His goal was to shorten the notation for all predefined strings in the rest of the code. public struct PredefinedStrings { public const string VeryLongName = "Very Long Name"; public const string AnotherVeryLongName = "Another Very Long N...

Why are static classes used?

I have doubts on static class and static methods. From MSDN I understood that "Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class." So if we don't want to associate a class over an instance , we will make it as static. Is that the only advantage? Can anyo...

Application service layer as static classes

In my ASP.NET MVC application, I have a project that contains all the business logic/service layer. This project interacts with my Database (Entity framework) which is located in a separate project. I wanted easy access to the service layer, so I created static classes in it so they can be referenced easily. For example, if I'm in my co...

Why can't I create extension methods for static classes?

When I try to create an extension method for the File class, I get an error telling me that I cannot do this because the class is static. However, I don't see why this stops the creation of an extension method, what implication is there? Thanks ...

C# - Which is more efficient and thread safe? static or instant classes?

Consider the following two scenarios: //Data Contract public class MyValue { } Scenario 1: Using a static helper class. public class Broker { private string[] _userRoles; public Broker(string[] userRoles) { this._userRoles = userRoles; } public MyValue[] GetValues() { return BrokerHelper.GetV...

Could/Should I use static classes in asp.net/c# for shared data?

Here's the situation I have: I'm building an online system to be used by school groups. Only one school can log into the system at any one time, and from that school you'll get about 13 users. They then proceed into a educational application in which they have to co-operate to complete tasks, and from a code point of view, sharing variab...

get_called_class hack not working with eval-code.

Hi there. I am using a ge_called_class hack for allowing late static binding in php version 5.2 (found here). I have the following in my code: # db_record.php $ac = "ForumThread"; $objects = $ac::find("all"); This will not work in php 5.2 for some reason, so I have done this: # db_record.php $ac = "ForumThread"; eval("\$objects =...

Why does Android prefer static classes

I see a lot of java code where android prefers to have developers use static inner classes. Particularly for patterns like the ViewHolder Pattern in custom ListAdapters. I'm not sure what the differences are between static and non-static classes. I've read about it but it doesn't seem to make sense when concerned with performance or mem...

Which static class initialize first ?

Hello everybody Which static class initialize first if we have one more static classes in our project? For example : Below code gives null exception. class Program { static void Main(string[] args) { First.Write(); Second.Write(); } } static class First { public s...

When to use static classes and methods?

I have a general question...when should i be using static classes or static methods?.. I know the idea that static methods can be called without instantiating...and static classes should only be used for static methods?...but are there any performance concerns also with it...and when should they be preferred over instance methods and cla...

using static classes for global objects in C#

I am using a list for particles. List<Particle> particles; Normally i place this list in my Simulation class. Which calculates position, velocity and other properties of particles. A few other classes need this particle data for output and post processing. is it OK to create a static class, static class Particles { static List<...

Where does static classes/members allocate?

Hi, it's being a long time since i'm trying to find out the truth about static classes. my point is: value types are allocated in stack, reference types in heap, when using the new operator. but a nature of a static class is that you can't make an instance of it, and sure it's not a value type. so i have a question when and where does th...

How can I call a non-static method from another class in Java?

Okay, this is a bit messy: I'm using Netbeans, and I have a main class called ParameterUI. (This is a GUI) This class has a few sliders on its GUI, and since these are private, I have a method called getBounds(). I don't want to clutter up my GUI, and so essentially all the important methods for calculating stuff are in another class ca...

How to force static class to implement specific methods?

I need to create a set of static classes and all of them need to implement the same methods. I want to find a way to force them so. I understand that static classes cannot derive anything other than System.Object. Should I use non-static methods for this? It could be, but none of the methods of this class will use instance properties......

I once read that static classes are very difficult and even impossible to debug. Is this true and why?

I once read that static classes are very difficult and even impossible to debug. Is this true and why? If an example would help, here is a PHP class I use to access a database (I don't think this is a PHP-specific question, though): <?php class DB { private static $instance; private function __construct() { } public stat...