static-methods

Deciding to make a C# class static

这一生的挚爱 提交于 2019-12-05 20:19:37
If I have a Utilities.cs class in C#, which contains only static methods e.g. public static string DoSomething() { return "something"; } Should I make the class itself static? public static Utilities() { ... Does making the class static have any advantages? Ray Yes you should. It stops people from instantiating the class when they shouldn't, and makes the purpose of the class clearer. Have a read of the MSDN page for more information . You also need to mark the class as static if you want to add Extension Methods You should think carefully about utility classes. Think whether the methods truly

Wrapping an API to support dependency injection

那年仲夏 提交于 2019-12-05 18:49:49
I am interacting with an API that just has static functions, and cannot be opened up and changed. public class WindowsNativeGraphAPI { public static IEnumerable<IGraphData> GetGraphData(); public static bool DeleteGraphData(IGraphData data); } I would like to be able to pass the API into a function or constructor and comply with dependency injection (just in case we were to swap out the API later). public void GatherGraphData(IGraphAPI api) {...} To allow this API to be passed in as a parameter, I'd need to at least abstract to use an interface to pass into the function. public interface

error :should not be called statically, assuming $this from incompatible context. only on my machine

会有一股神秘感。 提交于 2019-12-05 15:25:20
My team members wrote the model function calls in the controller statically such as: $data = ModelName::functionName($param); while it should be called dynamically such as: $model = new Model(); $data = $model->functionName($param); mostly all the calls are made statically. the code is working on the server and on their local machines except for mine. And the static calls are too many to fix without rewriting huge code base. I always update my project via composer. My php version is 5.4. anyone might know what this is about? Kleskowy You probably have PHP running with E_STRICT error reporting.

PowerMockito mock single static method and return object inside another static method

时光怂恿深爱的人放手 提交于 2019-12-05 15:17:54
I have written test cases to mock static classes and methods using PowerMockito's mockStatic feature. But I am strugling to mock one static method inside another static method. I did see few examples including this but none of them actually helping me or I am not understanding the actual functionality? (I'm clueless) Eg. I have a class as below and complete code is here . public static byte[] encrypt(File file, byte[] publicKey, boolean verify) throws Exception { //some logic here PGPPublicKey encryptionKey = OpenPgpUtility.readPublicKey(new ByteArrayInputStream(publicKey)); //some other logic

Can I import a static class as a namespace to call its methods without specifying the class name in C#?

☆樱花仙子☆ 提交于 2019-12-05 14:25:19
问题 I make extensive use of member functions of one specific static class. Specifying the class name every time I call it's methods looks nasty... Can I import a static class as a namespace to call its methods without specifying the class name C#? 回答1: If you mean import it such that it's methods are global, no. You might want to look at extension methods though. They are static methods that, when their class's namespace is imported, show up as instance methods on the type of their first argument

Should I never use static methods and classes and singletons when following the Test Driven Development paradigm

你。 提交于 2019-12-05 10:52:35
问题 I've been reading that static methods, static classes, and singletons are evil when you try to implement unit testing in your project. When following the TDD paradigm, should I just forget that they ever existed and never use them again or is it ok to use them sometimes? 回答1: Never say never--static classes and methods have their place in your toolbox. That said, if the class you are trying to isolate and test (subject under test or SUT) depends on a static class or method, you will be unable

Difference between Static function declaration and the normal function declaration in Javascript?

柔情痞子 提交于 2019-12-05 10:41:26
There are many ways one can declare a function in javascript. One of the ways is declaring a class and a static function inside is as showed below. class className { static fucntionName() { } } another way of is declaring is through the tradition javascript style as showed below. export function functionName() { } I would like to know the advantages/disadvantages of using either of the cases. Is there any specific use cases for the static methods, why declare a class(we know that in javascript there is no need to instantiate the class in order to access the static function). Why not just use

Static method and extension method with same name

你。 提交于 2019-12-05 09:25:37
I created extension method: public static class XDecimal { public static decimal Floor( this decimal value, int precision) { decimal step = (decimal)Math.Pow(10, precision); return decimal.Floor(step * value) / step; } } Now I try to use it: (10.1234m).Floor(2) But compiler says Member 'decimal.Floor(decimal)' cannot be accessed with an instance reference; qualify it with a type name instead . I understand there is static decimal.Floor(decimal) method. But it has different signature. Why compiler is unable to choose correct method? You have two good and correct answers here, but I understand

Can static methods in javascript call non static

烈酒焚心 提交于 2019-12-05 08:53:20
I am curious as I get a "undefined is not a function" error. Consider the following class: var FlareError = require('../flare_error.js'); class Currency { constructor() { this._currencyStore = []; } static store(currency) { for (var key in currency) { if (currency.hasOwnProperty(key) && currency[key] !== "") { if (Object.keys(JSON.parse(currency[key])).length > 0) { var currencyObject = JSON.parse(currency[key]); this.currencyValidator(currencyObject); currencyObject["current_amount"] = 0; this._currencyStore.push(currencyObject); } } } } currencyValidator(currencyJson) { if (!currencyJson

How to monkeypatch a static method? [duplicate]

风格不统一 提交于 2019-12-05 07:13:24
This question already has answers here : Pointers to static methods in Python (3 answers) Closed 6 years ago . While it's fairly simple to monkeypatch instance methods to classes, e.g. class A(object): pass def a(self): print "a" A.a = a doing this with another class's @staticmethod à la class B(object): @staticmethod def b(): print "static b" A.b = B.b results in A.b() yielding a TypeError : unbound method b() must be called with A instance as first argument (got nothing instead) Make A.b a static method and you should be fine: A.b = staticmethod(B.b) 来源: https://stackoverflow.com/questions