static-classes

How can I create a static class in Swift?

匆匆过客 提交于 2019-11-30 17:42:20
I'm looking to create a static class called VectorCalculator. Perhaps this function should just be placed in my Vector class (similar to NSString's - -stringByAppendingString method in Obj-C)... and if you think that... let me know. Anyway I want to add a couple of static functions to a static class called VectorCalculator. It will calculate the 'dot product' and return a Vector. Another function will likely be to 'calculate and return the angle between two given vectors'. A) Would anyone go this route of creating a static class for this or B) should I just add these functions as instance

Refactoring a static class to use with dependency injection

ε祈祈猫儿з 提交于 2019-11-30 12:11:09
问题 We need to use an unmanaged library in our code that has static methods. I'd like to introduce the library operation as a dependency in my code. And apart from having static methods, the library has an initialization method and a settings method, both are global. So I can't just wrap this in an instance class, because if one instance changes a setting, all other instances will be affected, and if one instance gets initialized, all other instances will be reinitialized. I thought about

Error: Extension methods must be defined in a top level static class (CS1109)

ε祈祈猫儿з 提交于 2019-11-30 06:02:38
问题 I'm trying to make a countdown program, which I can start and stop and set the value of the countdown to 10 minutes if needed. But I'm getting an error I don't quite understand. I'm not that into C#, so here's the code: Can some one help me a bit here ? Think I run on framework 3.0 or something ? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System

Exception in static constructor

六眼飞鱼酱① 提交于 2019-11-30 02:46:57
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"); } mailHost = ConfigurationManager.AppSettings["mailHost"]; if (null == mailHost) { throw new

How to serialize non-static child class of static class

岁酱吖の 提交于 2019-11-30 02:26:41
问题 I want to serialize a pretty ordinary class, but the catch is it's nested in a static class like this: public static class StaticClass { [Serializable] public class SomeType { ... } } This code: StaticClass.SomeType obj = new StaticClass.SomeType(); XmlSerializer mySerializer = new XmlSerializer(typeof(obj)); Produces this error: StaticClass.SomeType cannot be serialized. Static types cannot be used as parameters or return types. That error seems completely irrelevant; StaticClass.SomeType is

Refactoring a static class to use with dependency injection

☆樱花仙子☆ 提交于 2019-11-30 02:03:44
We need to use an unmanaged library in our code that has static methods. I'd like to introduce the library operation as a dependency in my code. And apart from having static methods, the library has an initialization method and a settings method, both are global. So I can't just wrap this in an instance class, because if one instance changes a setting, all other instances will be affected, and if one instance gets initialized, all other instances will be reinitialized. I thought about introducing it as a singleton class. This way it will be in an instance class, but there will only be one

How can I create a static class in Swift?

二次信任 提交于 2019-11-30 01:50:19
问题 I'm looking to create a static class called VectorCalculator. Perhaps this function should just be placed in my Vector class (similar to NSString's - -stringByAppendingString method in Obj-C)... and if you think that... let me know. Anyway I want to add a couple of static functions to a static class called VectorCalculator. It will calculate the 'dot product' and return a Vector. Another function will likely be to 'calculate and return the angle between two given vectors'. A) Would anyone go

ASP.NET Core Web API Logging from a Static Class

这一生的挚爱 提交于 2019-11-29 17:06:46
问题 I'm logging just fine using dependency injection on my controllers, now I need to log something from a static class. How can I log from a static class? I can't use dependency injection because it's static and I can't just pass in an existing logger object to the static class because it would have the wrong name of class in the log file. In other words how can I get a logger from the loggerfactory inside my static class? I came across a similar question and they pointed to an article on a

How can I run a static initializer method in C# before the Main() method?

白昼怎懂夜的黑 提交于 2019-11-29 13:10:12
Given a static class with an initializer method: public static class Foo { // Class members... internal static init() { // Do some initialization... } } How can I ensure the initializer is run before Main() ? The best I can think of is to add this to Foo : private class Initializer { private static bool isDone = false; public Initializer() { if (!isDone) { init(); isDone = true; } } } private static readonly Initializer initializer = new Initializer(); Will this work or are there some unforeseen caveats? And is there any better way of doing this? Simply do the initialization inside a static

What is a “static” class?

微笑、不失礼 提交于 2019-11-28 16:50:30
In C# what is the difference between: public static class ClassName {} And: public class ClassName {} lmsasu A static class cannot be instantiated, and can contain only static members. Hence, the calls for a static class are as: MyStaticClass.MyMethod(...) or MyStaticClass.MyConstant . A non static class can be instantiated and may contain non-static members (instance constructors, destructor, indexers). A non-static member of a non-static class is callable only through an object: MyNonStaticClass x = new MyNonStaticClass(...); x.MyNonStaticMethod(...); Firstly, a comment on an answer asked