initializer

static readonly field initializer vs static constructor initialization

时光怂恿深爱的人放手 提交于 2019-11-27 18:21:11
Below are two different ways to initialize static readonly fields. Is there a difference between the two approaches? If yes, when should one be preferred over the other? class A { private static readonly string connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } class B { private static readonly string connectionString; static B() { connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } } Mark Byers There is one subtle difference between these two, which can be seen in the IL code - putting an explicit

Static constructor equivalent in Objective-C?

本小妞迷上赌 提交于 2019-11-27 17:22:30
I'm new to Objective C and I haven't been able to find out if there is the equivalent of a static constructor in the language, that is a static method in a class that will automatically be called before the first instance of such class is instantiated. Or do I need to call the Initialization code myself? Thanks The +initialize method is called automatically the first time a class is used, before any class methods are used or instances are created. You should never call +initialize yourself. I also wanted to pass along a tidbit I learned that can bite you down the road: +initialize is inherited

How to handle a static final field initializer that throws checked exception

笑着哭i 提交于 2019-11-27 12:36:03
I am facing a use case where I would like to declare a static final field with an initializer statement that is declared to throw a checked exception. Typically, it'd look like this: public static final ObjectName OBJECT_NAME = new ObjectName("foo:type=bar"); The issue I have here is that the ObjectName constructor may throw various checked exceptions, which I don't care about (because I'd know my name is valid, and it's allright if it miserably crashes in case it's not). The java compiler won't let me just ignore this (as it's a checked exception), and I would prefer not to resort to: public

Static class initializer in PHP

北战南征 提交于 2019-11-27 10:27:00
I have an helper class with some static functions. All the functions in the class require a ‘heavy’ initialization function to run once (as if it were a constructor). Is there a good practice for achieving this? The only thing I thought of was calling an init function, and breaking its flow if it has already run once (using a static $initialized var). The problem is that I need to call it on every one of the class’s functions. Peter Bailey Sounds like you'd be better served by a singleton rather than a bunch of static methods class Singleton { /** * * @var Singleton */ private static $instance

What Is a Curly-Brace Enclosed List If Not an intializer_list?

依然范特西╮ 提交于 2019-11-27 09:00:57
I asked a question here: Lifetime Extension of a initializer_list return involving the non-functional code: const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; }; I believed the lambda was trying to return an intializer_list (that's bad, don't do that.) But I got a comment : It's not an initializer_list , it's an initializer list. Two different things. I just thought that any time you did a curly-braced list you were creating an intializer_list . If that's not what's happening, what is a list in curly-braces? There are three distinct, but related concepts here:

CoreData: error: Failed to call designated initializer on NSManagedObject class

被刻印的时光 ゝ 提交于 2019-11-27 07:57:43
I have a little damn problem with CoreData. I want to insert a new Object, so I first have to create one. This is done by that code: Challenges *newChallenge = [[Challenges alloc] init]; [newChallenge setName:@"TestChallenge"]; [newChallenge setRounds:[[NSNumber alloc] initWithInt:12]]; [newChallenge setShots:[[NSNumber alloc] initWithInt:5]]; [newChallenge setDate:[NSDate date]]; But however after the alloc init I get this error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'Challenges' What the hack is going wrong? duDE I think the problem is that

How to create initializer to create and migrate mysql database?

半腔热情 提交于 2019-11-27 07:19:33
I have been learning how to use EF for a week or so now and am stuck on the issue of creating/updating my database. I am able to create an initializer to create the database if it is not there: static class Program { static void Main() { Database.SetInitializer<GumpDatabase>(new GumpDatabaseInitializer()); .... class GumpDatabaseInitializer : CreateDatabaseIfNotExists<GumpDatabase> { public GumpDatabaseInitializer() { } protected override void Seed(GumpDatabase context) { context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX Name ON Stations (Name)"); // Other stuff } } Or I can create a

Initializer does not override a designated initializer from its superclass

▼魔方 西西 提交于 2019-11-27 07:04:53
So I've just upgraded to Xcode 6.3 Beta 3 and a lot of error(s) are appearing relating to the following: Initializer does not override a designated initializer from its superclass. override init() { super.init() } For example this is a UIButton class: class CustomButton: UIButton { var target: AnyObject! var selector: Selector! var action: (() -> Void)! override init() { // Initializer does not override a designated initializer from its superclass super.init() // Must call a designated initializer of the superclass 'UIButton' } required init(coder aDecoder: NSCoder) { super.init(coder:

What is a designated initializer in C?

喜夏-厌秋 提交于 2019-11-27 06:04:26
问题 I know this might be a basic question. I have an assignment that requires me to understand what designated initializers in C are and what it means to initialize a variable with one. I am not familiar with the term and couldn't find any conclusive definitions. What is a designated initializer in C? 回答1: Designated initialisers come in two flavours: 1) It provides a quick way of initialising specific elements in an array: int foo[10] = { [3] = 1, [5] = 2 }; will set all elements to foo to 0,

How to check if database schema matches Entity Framework schema?

旧街凉风 提交于 2019-11-27 05:59:00
问题 For my surprise, using the CreateDatabaseIfNotExists context initializer, the line context.Database.Initialize(true) doesn't throw an exception if the schema does not match my code first schema. Is there a way to validate if the current database matches our schema before, for instance, we try to access a entity, whose table doesn't exist on the database anymore, and an exception is thrown by EF? 回答1: You can call CompatibleWithModel to determine if the database matches the model. If you set