static-classes

Why can't a static nested class access “this” pointer of outer class?

半世苍凉 提交于 2019-12-11 04:47:27
问题 The thing that bothers me is the second point. I thought it might have to do with the fact that the "this" pointer isn't static and so the inner class can't access it. I'm not sure if that's the right explanation though. This also raised another question for me which was "where is the "this" pointer defined?" 回答1: The difference between a static nested class and one that isn't static is precisely that an instance of a non- static inner class is associated with a specific instance of the

How to make pluggable static classes

安稳与你 提交于 2019-12-10 18:25:39
问题 I know how to make pluggable things in c#. Define an Interface, Activator.CreateInstance(<class>) , etc. Or I can have a code path that explicitly creates an instance of the plugged class. Many ways. But what if the service I want to make pluggable is static (I know I could refactor so that it's not but that's not the point of the question) Concrete example. I have a class that provides disk I/O abstraction (Read File, List Directory,....). Now I want different implementations of this

What's the difference between enums & using static classes with constants?

馋奶兔 提交于 2019-12-10 10:19:59
问题 What are the performance implications between these two items? I've seen the static class in the wild recently and I'm not sure what to make of it. public enum SomeEnum { One = 1, Two, Three } public static class SomeClass { public static readonly int One = 1; public static readonly int Two = 2; public static readonly int Three = 3; } 回答1: The difference is type safety. Suppose you have two of these enums. How are you going to tell the difference: void SomeMethod(int x, int y) // Compiles,

How to inject into static classes using Dagger?

青春壹個敷衍的年華 提交于 2019-12-10 02:12:04
问题 I want to introduce dependency injection through Dagger to a project. The following code acts as an example to describe the problem of injection into static classes . The static method setupTextView() is called from multiple classes: public abstract class TextViewHelper { public static void setupTextView(TextView textView, Spanned text, TrackingPoint trackingPoint) { textView.setText(text, TextView.BufferType.SPANNABLE); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView

Exception in static constructor

女生的网名这么多〃 提交于 2019-12-08 23:04:59
问题 I've dug around SO for an answer to this, and the best one I can find so far is here, however that is geared toward instances with static constructors; I'm only using the class statically. My code: public static class MailHelper { private static string mailHost; static MailHelper() { var mailSettings = ConfigurationManager.GetSection("MailSettings") as NameValueCollection; if (null == mailSettings) { throw new ConfigurationErrorsException("Missing Mail Settings in the configuration file"); }

Are static classes supported by Swift?

。_饼干妹妹 提交于 2019-12-08 01:38:36
问题 I would like to know if you can create static classes in swift similar to those in C# i.e. a class that cannot be instantiated which can only have static functions and static properties. Are these types of classes possible in swift? If not, what is the most efficient way to recreate such a design pattern given the tools available within Swift? 回答1: No, Swift has no concept of a static class. But if you have no assignable property anyway, what difference will initialization make? I can think

Having separate copy of base class static member in each derived class

感情迁移 提交于 2019-12-07 10:04:04
问题 I have following class structure: public abstract class PresenterBase { public static Dictionary<string, MethodInfo> methodsList; public void Bind() public void Initialize(); } public class DevicePresenter: PresenterBase { public void ShowScreen(); public void HandleEvents(); } public class HomePresenter: PresenterBase { public void ShowScreen(); public void HandleEvents(); } I want to have HomePresenter and DevicePresenter to have separate copy of methodsList static member defined in

Are static classes supported by Swift?

≯℡__Kan透↙ 提交于 2019-12-06 08:27:43
I would like to know if you can create static classes in swift similar to those in C# i.e. a class that cannot be instantiated which can only have static functions and static properties. Are these types of classes possible in swift? If not, what is the most efficient way to recreate such a design pattern given the tools available within Swift? No, Swift has no concept of a static class. But if you have no assignable property anyway, what difference will initialization make? I can think of 2 options: Mark the class as final so it cannot be inherited: final class MyClass { .... } Use a struct ,

C++ High performance unit testing with Google Mock?

被刻印的时光 ゝ 提交于 2019-12-06 04:01:15
问题 I'm using Google Mock, and I'm struggling to mock out C++ system calls (specifically the C++11 chrono functions). I'm know I'm supposed to make an interface, create a class to implement the interface for my actual implementation, and then mock out the interface on my tests. I'm trying to write an embedded application, so this level of indirection sounds too expensive for me. What is the most efficient/performant way to incorporate system calls into Google Mock? 回答1: No, you don't need to

Extending the Enumerable class in c#?

对着背影说爱祢 提交于 2019-12-06 03:29:13
问题 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 extension methods are accesible only through its objects. And it gives me an error 'System.Linq.Enumerable': static types cannot be used as parameters Can someonce clarify me how to do this