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

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. // vector.hpp class vector { vector(double x,…
0
votes
1 answer

Double assignement in object initializer

I just stumbled over the following: class AFrameworkElement : FrameworkElement { private void SomeMethod() { Size s = new Size { Width = Height = 10 }; // the size isn't used for measuring oder arranging …
HerpDerpington
  • 3,751
  • 4
  • 27
  • 43
0
votes
2 answers

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 { Anchor = AnchorStyles.Bottom }; But it doesn't with…
Andy
  • 3,997
  • 2
  • 19
  • 39
0
votes
2 answers

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 listbox(lb2). -They are two different tables that has…
0
votes
2 answers

Cannot implicitly convert type 'string' to 'System.Collections.Generic.ICollection'

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 to enter a value into textarea in throws this…
regeme
  • 872
  • 6
  • 17
  • 30
0
votes
3 answers

Difference between Initializer or Static Initiliazer?

When I was working with XmlDOM in Asp.Net, there was a pattern like this : `XmlReader reader = XmlReader.Create()". And then I encountered the same pattern several times later. I like to know what's the difference between Static Constructor and "new…
Tarik
  • 79,711
  • 83
  • 236
  • 349
0
votes
1 answer

How to initialize an property value depending on another propertey

I have the following code: var workOrderList = new List( from index in Enumerable.Range(1, orders.Length) select new WorkOrder { OrderID = orders[index - 1], …
Simsons
  • 12,295
  • 42
  • 153
  • 269
0
votes
5 answers

Avoid Duplicating Object Initializer Value

I have a number of objects in my project which have fields that will contain matching values when they're initialized - see the code snippet below for an example, in particular the AddedDateTime and ModifiedDateTime properties: User user = new…
Bern
  • 7,808
  • 5
  • 37
  • 47
-1
votes
1 answer

there is a problem adding entity in .net core

i have a problem in adding entity to database in .net core.all of my code execute with no error but any record inserts in database this is my adding service below public async Task AddHpl(IFormFile ClinicImgUp, AddHealthPlaceViewModel…
-1
votes
1 answer

how to initialize a non copyable object data member

I want to use an already implemented abstract class (which can be seen as "A" in my simple provided code) in my code. I defined class "B" to implement those pure virtual methods. The fact, here, is that the objects of this class cannot be copyable…
Adroit
  • 3
  • 2
-1
votes
1 answer

Regarding System.Object Constructor class in c#

Object class constructor is called in the constructor of the class when an object is created . What happens in the Object Constructor?
123
  • 41
  • 8
-1
votes
1 answer

C# | Syntax to Initialize New Object With Properties of Another Instance

C# Initialize Object With Properties of Another Instance (A) I can do this... var newRestaurant = new Restaurant(); newRestaurant.Cuisine = model.Cuisine; newRestaurant.Name = model.Name; (B) And I can write it this way... var newRestaurant = new…
Cody
  • 9,785
  • 4
  • 61
  • 46
-1
votes
2 answers

Why does nothing happen when I execute my module in a terminal?

I tried running my code in a Ruby script from my terminal. Nothing happens when I run ruby Main.rb. # Main.rb module Main class MyClass def initialize puts "Hello World" end end end
Nurdin
  • 23,382
  • 43
  • 130
  • 308
-1
votes
2 answers

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 #include using namespace…
-2
votes
2 answers

Object initialization for master/detail relationship

I have two classes representing a master detail relationship, where the master object contains the details and the detail object references the master. I'm trying to use object initialization to instantiate them, but not sure how to pass the master…
Jeremy
  • 44,950
  • 68
  • 206
  • 332
1 2 3
14
15