object-initializers

Can you Instantiate an Object Instance from JSON in .NET?

Since Object Initializers are very similar to JSON, and now there are Anonymous Types in .NET. It would be cool to be able to take a string, such as JSON, and create an Anonymous Object that represents the JSON string. Use Object Initializers to create an Anonymous Type: var person = new { FirstName = "Chris", LastName = "Johns...

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 (some MSBuild mutation) and when it comes to compiling the code for a part of the solution ...

Why I cannot use Object Initializers in ASP.NET 2.0 ?

Why I can use Object Initializers in Visual Studio 2008 Windows projects, etc targeted to .NET 2.0 but cannot - in ASP.NET projects targeted to .NET 2.0 ? I understand that this is C# 3.0 features, but don't - why this possible to use in .NET 2.0 projects. ...

Order of operations using Object Initializer Syntax

Does the order in which I set properties using the object initializer syntax get executed in the exact same order? For instance if I do this: var s = new Person { FirstName = "Micah", LastName = "Martin", IsLoaded = true } will each property get set in the same order? ...

What am I doing wrong with C# object initializers?

When i initialize an object using the new object initializers in C# I cannot use one of the properties within the class to perform a further action and I do not know why. My example code: Person person = new Person { Name = "David", Age = "29" }; Within the Person Class, x will equal 0 (default): public Person() { int x = Age; // ...

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 the above example to work with named constructors, such as: MyObjectValues .Select(curren...

C# 3.0 Object Initialation - Is there notification that the object is being initialized?

We have several domain objects which need to support both read-only and read-write modes; they currently have a bool Locked property for this--when Locked attempts to alter properties on the object result in an InvalidOperationException. The default state for the objects is Locked. The object-initialization syntax of C# 3 introduces a ...

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 <a href="DoSomething" someAttribute="a value">click me</a> My question is.... what if I want to set the "class" attribute? <%= Html.ActionLink("cl...

Difference between Initializer or Static Initiliazer ?

Hi Folks, 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 ClassName()" Constructor (I am not sure if I am using right terms to describ...

What is the name of this C# syntax?

In C#, you can do something like this: SomeClass someClass = new SomeClass () { SomeProperty = someValue }; What is this syntax called? ...

C++ undeclared identifier - object from .net dll class

I have a vb.net dll which I imported in an unmanaged c++ project. I successfully created an object of the class object using: CComPtr< IWSconnection > pIWSconnection; pIWSconnection.CoCreateInstance( __uuidof(IWSconnection ) ); Then, when I tried to call a method from the dll: pIWSconnection.connect(...); I am getting an erro...

Initializer syntax

Hi I like the C# 3 initializer syntax and use it a lot, but today while looking in Reflector, the following came up: var binding = new WSHttpBinding { ReaderQuotas = { MaxArrayLength = 100000 }, MaxReceivedMessageSize = 10485760 }; At first I thought it was a mistake, but it does compile! Guess I am still learning new stuff all t...

Objective-C Multiple Initialisers

I have a simple question about creating multiple initialisers within an objective-c class. Basically I have a class that represents a single row in my database (users). I currently have an initialiser which initialises the class based upon the users UserID (which is also the primary key within the database), when passed the UserID the cl...

How to change the formatting of the "Use Object Initializer" refactoring in Resharper?

When I refactor the following line: Employee e = new Employee(); e.First = "Frank"; e.Last = "Rizzo"; using Resharper's "Use Object Initializer", I get the following: Employee e = new Employee { First = "Frank", Last = "Rizzo" }; I really hate this type of formatting becau...

C# object initializer wanting to use wrong Add method

I have the following class hierarchy: public class Row : ICloneable, IComparable, IEquatable<Row>, IStringIndexable, IDictionary<string, string>, ICollection<KeyValuePair<string, string>>, IEnumerable<KeyValuePair<string, string>>, System.Collections.IEnumerable { } public class SpecificRow : Row, IXmlSerializable, ...

Object Initializer and Dynamically specifying properties

With an object initializer, is it possible to optionally include setting of property? For example: Request request = new Request { Property1 = something1, if(something) Property2 = someting2, Property3 = something3 }; ...

Is it possible to use Object Initializers on a bool?

Is it possible to do the following (e.g. initialize bool array and set all elements to true) in one line using object initializers? int weeks = 5; bool[] weekSelected = new bool[weeks]; for (int i = 0; i < weeks; i++) { weekSelected[i] = true; } I can't quite get it to work. Edit: I should have mentioned that I am using VS2008 ...

Code Contracts Vs. Object Initializers (.net 4.0)

At face value, it would seem that object initializers present a problem for .net 4.0 "code contracts", where normally the invariant should be established by the time the object constructor is finished. Presumably, however, object-initializers require properties to be set after construction is complete. My question is if the invariants ...

Initial capacity of collection types, i.e. Dictionary, List

Certain collection types in .Net have an optional "Initial Capacity" constructor parameter. i.e. Dictionary<string, string> something = new Dictionary<string,string>(20); List<string> anything = new List<string>(50); I can't seem to find what the default initial capacity is for these objects on MSDN. If I know I will only be storin...

C#: Object having two constructors: how to limit which properties are set together?

Say you have a Price object that accepts either an (int quantity, decimal price) or a string containing "4/$3.99". Is there a way to limit which properties can be set together? Feel free to correct me in my logic below. The Test: A and B are equal to each other, but the C example should not be allowed. Thus the question How to enfor...