object-initializers

Objective-C Multiple Initialisers

帅比萌擦擦* 提交于 2019-12-04 23:04:12
I have a simple question about creating multiple initialisers within an objective-c class. Basically I have a class that represents a single row in my database (users). I currently have an initialiser which initialises the class based upon the users UserID (which is also the primary key within the database), when passed the UserID the class will them connect to a webservice parse the results and return an object initialised to the corresponding row in the database. Within this database are a number of unique fields (username and emailaddress), I would also like to be able to initialise my

Optimizing multiprocessing.Pool with expensive initialization

自古美人都是妖i 提交于 2019-12-04 09:19:12
Here is a complete simple working example import multiprocessing as mp import time import random class Foo: def __init__(self): # some expensive set up function in the real code self.x = 2 print('initializing') def run(self, y): time.sleep(random.random() / 10.) return self.x + y def f(y): foo = Foo() return foo.run(y) def main(): pool = mp.Pool(4) for result in pool.map(f, range(10)): print(result) pool.close() pool.join() if __name__ == '__main__': main() How can I modify it so Foo is only initialized once by each worker, not every task? Basically I want the init called 4 times, not 10. I am

Constructor vs Object Initializer Precedence in C#

丶灬走出姿态 提交于 2019-12-04 08:01:08
问题 I've been learning the object initializer in C# recently, but now I'm wondering how it works when it conflicts with the constructor. public class A { public bool foo { get; set; } public A() { foo = true; } public A(bool bar) { foo = bar; } } What happens when I try this? public class B { a = A() {foo = false}; b = A(true) {foo = false}; } Is a default in the constructor a good way to have a bool that starts true and can be changed? public A(bar=true) { foo = bar; } 回答1: From the

CodeDom and collection initializers

家住魔仙堡 提交于 2019-12-03 20:12:48
Is there a way to generate a dictionary initializer using the C# CodeDom? Are those supported at all? I would like to have: private IDictionary<string, string> map = new Dictionary<string, string> { { "Name", "Value" }, ... }; This is not possible using the CodeDom constructs. They were not updated for collection initializers. LukeH has an excellent blog post on the subject of 3.5 features and the CodeDom http://blogs.msdn.com/b/lukeh/archive/2007/07/11/c-3-0-and-codedom.aspx You can do it but its possibly the worst nightmare ever.. Here is how I am doing it currently. (Updated my answer to

How to debug object initializer code?

为君一笑 提交于 2019-12-03 10:23:26
Is there a way to step by step debug the object initializer code in Visual Studio? Example: return new Veranstaltung() { ID = tblVeranstaltung.VeranstaltungsID, Titel = tblVeranstaltung.Titel, KursNummer = tblVeranstaltung.Kursnummer, ErsterTermin = tblVeranstaltung.ersterTermin, Dauer = tblVeranstaltung.schulungsTage, StartZeit = tblVeranstaltung.BeginnZeit, EndZeit = tblVeranstaltung.Endzeit, KostenNettoValue = tblVeranstaltung.PreisNetto ?? default(decimal), IsLastMinute = tblVeranstaltung.lastMinute == 1, IsVerkuerzt = tblVeranstaltung.istVerkuerzt == 1, IsGeschlossen = tblVeranstaltung

Constructor vs Object Initializer Precedence in C#

不羁岁月 提交于 2019-12-02 18:42:14
I've been learning the object initializer in C# recently, but now I'm wondering how it works when it conflicts with the constructor. public class A { public bool foo { get; set; } public A() { foo = true; } public A(bool bar) { foo = bar; } } What happens when I try this? public class B { a = A() {foo = false}; b = A(true) {foo = false}; } Is a default in the constructor a good way to have a bool that starts true and can be changed? public A(bar=true) { foo = bar; } From the documentation : The compiler processes object initializers by first accessing the default instance constructor and then

C# Object Initializer : Set Property from another one

纵饮孤独 提交于 2019-12-01 19:30:13
I have the following object where in my constructor I add a new Guid as the Id. public class MyObject { public MyObject() { Id = Guid.NewGuid().ToString(); } public String Id { get; set; } public String Test { get; set; } } I want to do something like that in an object initializer : var obj = new MyObject { Test = Id; // Get new GUID created in constructor } Is it possible? Jon Skeet No, you can't do that. You'd have to just set it in a separate statement: var obj = new MyObject(); obj.Test = obj.Id; The right-hand side of the property in an object initializer is just a normal expression, with

Object Initializer and Dynamically specifying properties

我们两清 提交于 2019-12-01 04:24:14
With an object initializer, is it possible to optionally include setting of property? For example: Request request = new Request { Property1 = something1, if(something) Property2 = someting2, Property3 = something3 }; Not that I'm aware of. Pretty sure your only option is to do it like this: Request request = new Request { Property1 = something1, Property3 = something3 }; if(something) request.Property2 = someting2; Or you could do it like this if there's a default/null value you can set it to: Request request = new Request { Property1 = something1, Property2 = something ? someting2 : null,

Object Initializer and Dynamically specifying properties

瘦欲@ 提交于 2019-12-01 02:54:09
问题 With an object initializer, is it possible to optionally include setting of property? For example: Request request = new Request { Property1 = something1, if(something) Property2 = someting2, Property3 = something3 }; 回答1: Not that I'm aware of. Pretty sure your only option is to do it like this: Request request = new Request { Property1 = something1, Property3 = something3 }; if(something) request.Property2 = someting2; Or you could do it like this if there's a default/null value you can set

Can you Instantiate an Object Instance from JSON in .NET?

ⅰ亾dé卋堺 提交于 2019-11-30 13:42:14
Since Object Initializers are very similar to JSON, and now there are Anonymous Types in .NET. It would be cool to be able to take a string, such as JSON, and create an Anonymous Object that represents the JSON string. Use Object Initializers to create an Anonymous Type: var person = new { FirstName = "Chris", LastName = "Johnson" }; It would be awesome if you could pass in a string representation of the Object Initializer code (preferably something like JSON) to create an instance of an Anonymous Type with that data. I don't know if it's possible, since C# isn't dynamic, and the compiler