Questions tagged [object-initializers]

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.

object-initializers
Filter by
Sorted by
Tagged with
0 votes
1 answer
840 views

C# object initializer [duplicate]

I am trying to understand some code for my programming exam and I've stumbled upon this notation that I can't seem to find the explanation for. I've searched stackoverflow, msdn and several online ...
dzenesiz's user avatar
  • 1,444
0 votes
2 answers
210 views

Visual Studio 2015 missing new keyword - no compiler error

I was working on one of our new applications when I made a stupid mistake… I forgot to write: ”new ClassName” inside an object initializer. Strangely VS just compiled the code… without any error or ...
musium's user avatar
  • 2,982
2 votes
1 answer
2k views

How do you create a SwiftyJSON dictionary out of optional AnyObjects?

I recently ran into a very time-consuming issue for a simple task of creating a JSON dictionary with optional objects. I'm using SwiftyJSON. If I create the following dictionary, I get thrown errors ...
trixmasta's user avatar
  • 233
-1 votes
2 answers
57 views

Compiler Errors including initializer before '<' token

I am writing this piece of code for one of the Project Euler puzzles to practice coding and I am having trouble with what I think are a couple syntax errors. What am I doing wrong here? #include <...
Onpetcow's user avatar
-8 votes
2 answers
2k views

How can I create a SIP connecton using Liblinphone interface class?

Want to develop a SIP client for Android using Liblinphone library. There is a LinphoneAuthInfo class that accepts authentication. And a LinphoneCore.addAuthInfo() That adds authentication info to the ...
rostamiani's user avatar
  • 2,997
18 votes
1 answer
2k views

Why does this nested object initializer throw a null reference exception?

The following testcase throws a null-reference exception, when it tries to assign Id to an object which is null, since the code is missing the "new R" before the object initializer. Why is this not ...
Hugo Hallqvist's user avatar
0 votes
1 answer
31 views

Creating and Initializing a Generic Class

How can I create and initialize the following class? Specifically I'm struggling with TSortKey. I thought it would be something like: var p = new QueryParameters<Employees, e => e.LastName>...
Jay Pondy's user avatar
  • 176
5 votes
1 answer
4k views

Elasticsearch.NET NEST Object Initializer syntax for a highlight request

I've got: var result = _client.Search<ElasticFilm>(new SearchRequest("blaindex", "blatype") { From = 0, Size = 100, Query = titleQuery || ...
notsoobvious's user avatar
22 votes
5 answers
9k views

Can properties inside an object initializer reference each other?

Is it somehow possible for properties to reference each other during the creation of a dynamic object an anonymously-typed object (i.e. inside the object initializer)? My simplified example below ...
user3199179's user avatar
1 vote
2 answers
546 views

How to make a formatted string property into a linq-to-entity friendly expression?

In a recent EF Code First project we were attempting to optimize some Linq queries using different techniques (don't worry its not premature). One common way to optimize a linq query is to convert ...
Mark Rogers's user avatar
  • 97.2k
0 votes
2 answers
185 views

Catch-22 with creating a pan gesture recognizer as a constant?

I have a custom subclass of UIView. It has a pan gesture recognizer that I've set up as a required constant: let dragger: UIPanGestureRecognizer It's a constant because it's created once at ...
Duncan C's user avatar
  • 130k
2 votes
1 answer
199 views

What should a C# object initializer for a Dictionary<string, string[]> look like?

I am trying to declare a dictionary whose values are string arrays. How can I do this? I tried the following code (which does not work): Dictionary<string, string[]> NewDic = new Dictionary<...
Gaduks's user avatar
  • 681
0 votes
1 answer
516 views

Is there a way to initialize properties after construction of object?

I have a Conversion class like this: public class Conversion { public memorySource MSource { get; set; } public Rule[] Rules { get; set; } public Conversion(XElement xElement) { ...
YouneL's user avatar
  • 8,336
3 votes
0 answers
614 views

Have to initialize an object with nested properties and child properties in c#

Have to do recursive loop to find all the nested properties from a class till the last property and initialize them with a default value, if string then update with string.empty, if Enum then update ...
rcube9's user avatar
  • 41
0 votes
4 answers
283 views

Modelling large list of properties in C#

I have a list of 50 attributes that an object can have. This object can have different categories of properties - category 1, category 2 and category 3. Category 1 is subset of category 2, category 2 ...
blissfool's user avatar
  • 1,007
100 votes
8 answers
22k views

Best practice to implement a failable initializer in Swift

With the following code I try to define a simple model class and it's failable initializer, which takes a (json-) dictionary as parameter. The initializer should return nil if the user name is not ...
Kai Huppmann's user avatar
  • 10.8k
6 votes
2 answers
6k views

Populate array directly through object initializer [closed]

I have these 2 classes: class Customer { public string Name; public string City; public Order[] Orders; } class Order { public int Quantity; ...
Milkncookiez's user avatar
  • 7,067
3 votes
1 answer
336 views

Initialization of get-only property in owner-object initializer

last time I came across a construct in C# Object Initializer: having: public class Class1 { public Class2 GetterOnlyProperty { get; private set; } public Class1() { this....
Wojtek's user avatar
  • 135
0 votes
1 answer
500 views

Adding additional init code to an inherited initializer from superclass

I have a Swift UIView class (named HypnosisView) that draws a circle on the screen. The frame of the view is set to fill the screen. I would like to programmatically set the background color of the ...
dcgoss's user avatar
  • 2,177
5 votes
1 answer
1k views

Resharper - use object initializer refactor - how to retain parentheses on constructor call?

When I use Resharper to refactor my code to use an Object initializer, it reformats the code correctly as thus, the following code var response = new Response(); response.Value = "My value"; ...
Patrick Szalapski's user avatar
0 votes
1 answer
46 views

What is the added value of using object initializer?

What is the added value of using an object initializer? Is there any difference using it on value types compared to reference types? I have installed ReSharper recently, and for the following ...
Shachaf.Gortler's user avatar
1 vote
1 answer
53 views

Where is this variant on object initializers documented?

I did not see any explanation of this on MSDN:Object and Collection Initializers. Specifically, I've noted that it is possible to use an object initializer to set a property on a subType, rather than ...
Brian's user avatar
  • 25.7k
15 votes
3 answers
3k views

C# object initialization syntax in F#

Please note: this question is not the same as this question. I recently came across some C# syntax I hadn't previously encountered: Is there any way to do this in F#? class Two { public string ...
N_A's user avatar
  • 19.8k
0 votes
1 answer
61 views

Functions & Property Usage in Object Initializers

Does the spec of C# prevent calling a method from within an object's (or struct's) initializer construct? The reason I'm asking is because I was trying to use a LINQ-to-XML statement to use gater ...
JNYRanger's user avatar
  • 6,979
32 votes
2 answers
18k views

Use object initializer - Resharper suggestion

I use ReSharper everyday, and today I asked myself why ReSharper suggests "Use object initializer" when I do this : MyClass myClass = new MyClass(); myClass.MyInt = 0; myClass.MyString = string.Empty;...
Wassim AZIRAR's user avatar
1 vote
4 answers
180 views

How to access an object in c#?

I'm having an object named data,which belongs to the class, [DataContract] public class names { [DataMember(Name = "code")] public int Code { get; set; } [DataMember(Name = "message")] ...
Aju's user avatar
  • 796
3 votes
5 answers
1k views

Parenthesis with object initializers

In C#, I can have a class such as: class MyClass { public void DoStuff(); } I can initialize this class like: MyClass mc = new MyClass(); Or: MyClass mc = new MyClass { }; If I don't need the ...
CodingIntrigue's user avatar
0 votes
3 answers
4k views

C# Object initializer without default constructor

I am trying to create an object using c# object initializer. But that class already has a constructor with one argument as per the need and I dont have a need for a default construcor so I didn't ...
Alagesan Palani's user avatar
0 votes
2 answers
2k views

C++ Object Constructor with Temporary Object as Argument

I've written some code and it suddenly occurred to me that what I'm doing might be a really bad idea. Here is a sort of abstract example. The key point is the passing by reference of the vector. // ...
FreelanceConsultant's user avatar
0 votes
1 answer
61 views

Double assignement in object initializer

I just stumbled over the following: class AFrameworkElement : FrameworkElement { private void SomeMethod() { Size s = new Size { Width = Height = 10 }; // ...
HerpDerpington's user avatar
5 votes
1 answer
376 views

Why does using an Object Initializer keep an object alive?

I recently came across this SO article and tweaked it for my scenario which follows: using System; using System.Collections.Generic; namespace ConsoleApplication18 { class Program { ...
Jim's user avatar
  • 4,920
3 votes
2 answers
759 views

Object Initializers, Non-Public Children, and Nested Initialization

I was looking through my codebase and found a line of code that R# had helpfully refactored for me. Here's a representative sample: public class A { public B Target { get; private set; } ...
eouw0o83hf's user avatar
  • 9,538
4 votes
1 answer
264 views

Object Initializer Formatting

Which setting(s) in Resharper 8 are reformatting my object initializer like this? (i.e. with the comma on it's own line). var snowDepthProcessor = new DataProcessor<SnowDepthModel> { Name ...
David Hayes's user avatar
  • 7,442
2 votes
1 answer
2k views

Overriding initialize method into model in Rails

I'm trying to override the initialize method. See below class Restriction < ActiveRecord::Base RESTRICTION_TYPES = { less_than: "IND<X", greater_than: "X<IND", between: "X<...
Cristhian Boujon's user avatar
0 votes
2 answers
84 views

Compile error / Object initializer with Type ChartArea

I am using .NET 4. I experience a strange behaviour with the object initializer combined with the ChartArea. The object initializer works with the Chart class: For example: Chart ch = new Chart { ...
Andy's user avatar
  • 4,057
1 vote
2 answers
461 views

Object Initializer vs Constructor in C# when many arguments are required

I'm developing a framework to access the EVE Online API from a C# Application. Essentially, the API works by the client sending a GET request to the EVE Online servers, the servers then send a ...
Zymus's user avatar
  • 1,698
0 votes
2 answers
3k views

Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'WebApplication.Title via a reference conversion, boxing conversion,

here is the situation. -There are two listboxes(lb1-lb2). In one(lb1) all data displayed that taken from database. -When I want to click data from lb1 the content has to be displayed in other ...
regeme's user avatar
  • 872
0 votes
2 answers
639 views

Cannot implicitly convert type 'string' to 'System.Collections.Generic.ICollection<WebApplication2.Entry>'

I used ado.net entity framework to connect database and have an .edmx file in project.. When I tried to reach objects in code side with object initializer I can see the object names but when I tried ...
regeme's user avatar
  • 872
67 votes
5 answers
24k views

Constructor vs Object Initializer Precedence in C#

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() ...
bbill's user avatar
  • 2,284
2 votes
1 answer
135 views

Can I use object initializer outside construction of an object?

Given object initializers: Foo foo = new Foo{ Name = "Jhon", Value = 2, IsMale = true }; Can they be somehow used elsewhere?(outside object construction) So that insted of using: foo.Name = "Name"; ...
Bosak's user avatar
  • 2,143
39 votes
1 answer
12k views

Nested object initializer syntax

Resharper has just suggested the following refactoring to me: // Constructor initializes InitializedProperty but // the UninitializedSubproperty is uninitialized. var myInstance = new MyClass(); ...
Rawling's user avatar
  • 49.7k
10 votes
4 answers
15k views

Should I use an object initializer or a constructor? [duplicate]

I have just learned about object initializers and was wondering what the best practices for when to use them are. This is what I read about them: http://msdn.microsoft.com/en-us/library/vstudio/...
Sarien's user avatar
  • 6,802
1 vote
2 answers
284 views

visual basic equivalent of "rectangle a=new a() { width=1; height=2; }"

rectangle a=new a() { width=1; height=2; } I used to construct objects like this, it there a similar way to do this in visual basic? I'm sorry I couldn't really label the technique.
Uğur Gümüşhan's user avatar
4 votes
2 answers
3k views

C# Object Initializer : Set Property from another one

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; ...
danbord's user avatar
  • 3,868
1 vote
2 answers
170 views

Creating an array of objects using initializers seems to fail

I have a GameObject we'll call the GM. Attached to it is a script that's meant to be the primary logical controller for a game. In that script, somewhere, I have: private dbEquipment ...
user462879's user avatar
1 vote
1 answer
2k views

How to assign a same value to different properties using Object Initializer

I try to assign a value to two different properties in object initializer and failed. In below code i try to assign Expand and Select properies to true. But i got the error 'The name Select ...
Murali Murugesan's user avatar
3 votes
2 answers
637 views

Are class initializers possible in C#?

In C# you have object initializers to initialize fields of objects at creation time without using a constructor. Now I'm wondering if there is an equivalent to classes which means that you can '...
Bastian's user avatar
  • 4,698
0 votes
1 answer
53 views

How to initialize an property value depending on another propertey

I have the following code: var workOrderList = new List<WorkOrder>( from index in Enumerable.Range(1, orders.Length) select new WorkOrder { ...
Simsons's user avatar
  • 12.5k
4 votes
1 answer
2k views

Object Initialization Not Working for Me

What am I missing here? I expected the following to work just fine: public class ProposalFileInfo { public int FileId { get; set; } public bool IsSupportDocument { get; set; } } // ... var ...
Jonathan Wood's user avatar
9 votes
5 answers
4k views

Removing the work from __init__ to aid unit testing

The key to this question is aiding unit-testing. If I have a busy __init__ (i.e. __init__ that does complex initialization), I cannot simply instantiate an object of a class, but I need to mock/stub ...