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
2
votes
1 answer

passing css class name to asp.mvc view helper

In ASP.NET MVC view helper, you can do something like <%= Html.ActionLink("click me", "DoSomething", null, new { someAttribute = "a value" } ) %> which will produce the following HTML click me My…
Adam
  • 913
  • 8
  • 12
2
votes
1 answer

unable to evaluate viewState against Null

This method I am using to fill the data source for a grid view, but for when getnew is false, it won't return any value , just returns a list with a single null value in it. private List GetAll_T(bool getNew) { if (getNew) …
joshua
  • 2,371
  • 2
  • 29
  • 58
2
votes
3 answers

Initializing array of structs within an array of structs

Given the following code: struct BufferPair { ByteBuffer _a; ByteBuffer _b; bool _c; }; struct TestData { MyClass _myClass; BufferPair _data[]; }; I'm trying to initialize an array of TestData where I also initialize an array…
MarkP
  • 4,168
  • 10
  • 43
  • 84
1
vote
2 answers

Using VB.NET object initializers in a factory pattern

I have written a base class from which I wish to derive several child classes (in this case Windows Form classes), and I'm using a Factory pattern in order to maintain a collection of the child instances, so that a form can only have one instance…
MCattle
  • 2,897
  • 2
  • 38
  • 54
1
vote
1 answer

using object initializer generates CA 2000 warning

Following code generates a CA2000 warning: Myclass myclass = null; try { myclass = new Myclass { Name = "a name" }; } finally { if (myclass != null) { myclass.Dispose(); } } i found some topics with the same problem and as I…
rhe1980
  • 1,557
  • 1
  • 15
  • 36
1
vote
5 answers

What to do with useless init?

This is currently what I have for my init, - (id)init { self = [super init]; if (self) { self.url = [[NSURL alloc] init]; self.blurb = [[NSString alloc] init]; self.author = [[NSString alloc] init]; } …
john
  • 3,043
  • 5
  • 27
  • 48
1
vote
1 answer

Creation of an instance with `with` block causes a type issue

I am using Groovy to create a package that I use in ReadyApi. In a Groovy script test step, I do the following: class B { String value boolean isSomething } class A { String name B propB public A() { this.name =…
1
vote
1 answer

Maximum call stack size exceeded - Object Initializer with reference to self

While playing around with a simple Javascript Object Initializer example, I could not find an explanation while the following code: const obj = { self: this }; console.log(obj); would lead an Error: Maximum call stack size…
tmarwen
  • 15,750
  • 5
  • 43
  • 62
1
vote
4 answers

calling an object method from another method not working

Suppose I have the following coding scenario: export const test = () => { return ( var1, var2, var3 ) => { return Object.freeze({ getVarOne: () => var1, getVarTwo: () => var2, …
user1790300
  • 2,143
  • 10
  • 54
  • 123
1
vote
2 answers

Set inner object's member as outer object using the object initialization syntax in C#

Context I have a List of type Question. Class Question, in turn, contains a List. Class Answer has a member called public Question Question { get; set; } which stores the question of which the answer is for. I'm using the collection…
Amal K
  • 4,359
  • 2
  • 22
  • 44
1
vote
2 answers

Why is a collection initializer without `new` allowed inside an object initializer but not outside?

I noticed strange behaviour when initializing collection property. Consider: class X { public IList Ints { get; set; } } I can initialize Ints like that: var theObject = new X { Ints = { 12, 3, 4, 5, 6 } }; But I cannot do that: var x…
Pawel
  • 525
  • 3
  • 16
1
vote
3 answers

Object Initialization and "Named Constructor Idiom"

Ok. So I have a list of values, and I'd like to do something like the following: MyObjectValues .Select(currentItems=>new MyType() { Parameter1 = currentItems.Value1, Parameter2 = currentItems.Value2 }); So here's the problem. I need…
GWLlosa
  • 23,995
  • 17
  • 79
  • 116
1
vote
0 answers

Type-hinted object initializer in parent constructor

Is it possible to have a type-hinted object initializer inside a constructor on a parent class? Ex: class Model { modelField: string; constructor(initializer: SomeSpecialType){ Object.assign(this, initializer); } } class User…
Ionel Lupu
  • 2,695
  • 6
  • 29
  • 53
1
vote
1 answer

C# Object Initializers and ConstructorInfo

Can anyone point me towards a solution for the following? I am trying to replicate a property attribute that uses Object Initializers by using the CustomAttributeBuilder; ie. [Display(Order = 0, Name = "UserNameLabel", ResourceType =…
Sam
  • 535
  • 5
  • 14
1
vote
3 answers

C++ Constructors with multiple raw pointers

I do not know how to make constructors for an object with multiple raw pointers. I understand how to make it for one but do not understand multiple. I have tried to write a new constructor during initialization, which then specifies what is accessed…