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.

An example of object initializers is the Object and Collection Initializers (C# Programming Guide):

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. The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax).

215 questions
0
votes
1 answer

Conversion of C# to VB.net List has error

I'm trying to convert some C# code to VB but I’m getting an error. What would be the correct VB syntax? C# return new List {"First Name", "Last Name", "First & Last Name", "None"}; VB Return New List(Of String)() From {"First Name", "Last…
0
votes
2 answers

C# Object Initializers and v2.0 compiler error

I'm having an issue setting up one of my projects in TeamCity (v4.0), specifically when it comes to using Object Initializers. The project builds fine normally, however it would seem that TeamCity transforms the build file into something it likes…
Matthew Savage
  • 3,794
  • 10
  • 43
  • 53
0
votes
0 answers

What happens if an initialiser returns an object of the wrong class?

Let's say I have an Objective-C class that's intended to be a singleton — its init method checks if there's already an instance of that class and returns it. Now suppose you have two subclasses of this class, A and B, and that you do: [[A alloc]…
0
votes
1 answer

Throw new exception using object initializer

Let's consider this code : try { return new ClassA.GetStuff(); } catch (Exception e) { throw new MyException ("message", e) {SomeMyExceptionProperty = "something"}; } When throwing MyException, how the object initialization…
ZwoRmi
  • 1,093
  • 11
  • 30
0
votes
1 answer

Set objects Property of type Dictionary while parsing dataset in foreach loop

So I have this problem. I have Project class with Name and Resource properties. I have DataSet and I want to set my Resource property values(of type Dictionary) from the DataSet. And tha's where I struggle. Any ideas how I could solve this…
Paul
  • 169
  • 13
0
votes
1 answer

C# object initializer

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 tutorials, but no luck. The code is something like…
dzenesiz
  • 1,388
  • 4
  • 27
  • 58
0
votes
3 answers

Linq IEnumerable Select Question - Can I do all of this inside my select?

I had a quick question. Can I do all of this logic inside the select statement? var entries = atisDAO.GetPME(xl, null); response.Data.Detectors = new List(entries.Select(pme => new DetectorDetails {ID =…
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
0
votes
1 answer

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 e.LastName>(); // WRONG!! public class QueryParameters
Jay Pondy
  • 176
  • 2
  • 11
0
votes
2 answers

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 initialization of the view and persists for the lifetime…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
0
votes
1 answer

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) { // I use Rules property here // From…
YouneL
  • 8,152
  • 2
  • 28
  • 50
0
votes
4 answers

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 is subset of category 3, and category 3 is the…
blissfool
  • 1,007
  • 1
  • 12
  • 21
0
votes
1 answer

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 view upon initialization (so when an instance of…
dcgoss
  • 2,147
  • 3
  • 20
  • 31
0
votes
1 answer

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 example: var response = new Response(); …
Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
0
votes
1 answer

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 data within the initializer. This does not work. …
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
0
votes
3 answers

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 provide one. Compiler too won't provide a default…
Alagesan Palani
  • 1,984
  • 4
  • 28
  • 53