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
2 answers

More concise way to pass, as constructor parameter, an Action that references private data?

This is stripped down from a more complex situation. The goal is to construct several instances of class SubAction, each of which uses an action to alter how it uses its internal data. Consider: public class SubAction { private…
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
0
votes
0 answers

How to initialize a struct/class that has huge array members?

Is there a way to initialize a C++ struct/class with very large array members? I would like to initialize them by filling them with any kind of data besides an empty array. The following code is defined in a dependency of mine: #define…
karobar
  • 1,250
  • 8
  • 30
  • 61
0
votes
2 answers

How to perform object initializer for a list property in VB.NET

I am trying to implement object initializer. All the classes are already created by someone else. I cannot change. I have a class Ins.vb that has a list as a property Partial Public Class Ins Private itemField As Item1 Private…
that_noob
  • 83
  • 1
  • 2
  • 13
0
votes
0 answers

Default member initializer not being set C++

I have the following code: class Foo { public: enum class MyEnum { option1, option2 } "Some constructors" private: "Some members and the following member" MyEnum enum_ = MyEnum::option1; } So basically, none of the…
A. Cimet
  • 87
  • 7
0
votes
1 answer

Are C++ aggregate initializers effective at run time or compile time?

TL;DR : I need global objects to be initialized at compile time, and I cannot use constexpr constructors. Can I use aggregate initializers for this task? I know that C/C++ aggregate initializers can be used to initialize an object's public members…
0
votes
0 answers

Possibility to rearrange properties in object initializer

I would like to rearrange my properties inside a object initializer, by manually defining the new position. I found this Question but the first method only sorts the properties alphabetically, the other uses a different extension method. new Student…
Stan1k
  • 338
  • 2
  • 17
0
votes
3 answers

Conditionally remove class member at run time or skip calling constructor of that member object

// globally accessible variable. can be changed at runtime. bool feature_flag = true Class A { UtilityClass obj_util; A(int x,int y,int z) { } } Class UtilityClass { UtilityClass() { } } Main() { A a(10,20,30); } When object to…
Sudheer
  • 81
  • 7
0
votes
1 answer

Concept regarding Upcasting, Inheritance and setting property using object initializer

The title of the question may seem confusing but bear with me, I'll try to explain the problem as clearly as possible. So I was just studying about the Liskov substitution principle from a course and the lecturer has given an example showing…
TejasGondalia
  • 168
  • 3
  • 13
0
votes
1 answer

Reference anonymous parent from anonymous child when declaring nested objects with Object Initializer

If I have a parent, child, grandchild set of classes, where the grandchild require's it's parent as a constructor parameter, is there any way to still be able to use a nested Object Initializers declaration? For example, consider Invoice Header,…
tomRedox
  • 28,092
  • 24
  • 117
  • 154
0
votes
0 answers

Is it possible to initalize a nested property when setting the parent?

I'd like to know if the following is possible and if it would have any performance benefits. Given this structure; public class X { [JsonIgnore] public List Y { get; set; } } public class Y { [JsonIgnore] public List Z { get;…
R3dn0l
  • 81
  • 1
  • 1
  • 11
0
votes
1 answer

Accessing property read value inside C# object initializer

I would like to reference a property on an object within an object initializer. The problem is that the variable does not yet exist, so I cannot reference it like normal (object.method). I do not know if there is a keyword to reference the object in…
ostler.c
  • 3,282
  • 3
  • 21
  • 21
0
votes
0 answers

Change property value during run of instantiated object

I am attempting to change the value of a property, throughout the course of running an instantiated object. This is part of a download mechanism where value 20 is set to perform a first download of data for 20 lines, the rest of downloads will be…
Toolbox
  • 2,333
  • 12
  • 26
0
votes
1 answer

Ruby on Rails -- commontator -- use avatar urls instead of paperclip avatar

So I'm using the commontator gem for comments in ruby and I wanna know how I can just use a simple url to display the avatar My user.avatar is basically a string with a url. Default: config.user_avatar_proc = lambda { |user, view| …
0
votes
2 answers

How can i get linq to sql to map my type when i use a parameterized constructor?

I know that L2S is not designed to map custom/POCO types to L2S-entity types without the object initializer syntax. But is there a back-book way to achieve this so that i can project into my POCO with a parameterized constructor and not get stung…
0
votes
1 answer

Adding to list with foreach loop

Is it possible something like this? I've worked hard, but I think it's not possible, I hope someone will help me. I have to fill the object like this, var obj = new { parentObj = new List() { outsideArray.ForEach(x=> { …
Caner
  • 813
  • 1
  • 12
  • 26