473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Object and collection initializes.

It seems simple enough to use objects initializers to initialize object
properties and collection intitializers to initialize collection, what I
have not been able to figure out is a way to initialize object properties
and collections at the same time.

By the way, I don't have a need to do that, I am simply curious if that is
doable or not. See snippet below.

Thank you.

class Program
{
static void Main(string[] args)
{
// Initialize property no problem.
var col2 = new MyCollection() { SomeProp = 123 };

// Initialize collection no problem.
var col1 = new MyCollection() { 1, 2, 4 };

// Initialize proerty and collection how????
//var p = new Pupu() {{ X = 123 }{1,2,3}};
}
}

class MyCollection : System.Collections.IEnumerable
{
private int m_SomeProp;
public int SomeProp
{
get { return m_SomeProp; }
set { m_SomeProp = value; }
}

internal void Add(object i)
{
}

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
// Prevent compiler error.
return null;
}
}

Oct 8 '08 #1
1 1445
Rene wrote:
It seems simple enough to use objects initializers to initialize object
properties and collection intitializers to initialize collection, what I
have not been able to figure out is a way to initialize object
properties and collections at the same time.
Bottom line: you can't combine them. If the collection is itself a settable
property you can initialize and assign one, but if it's a read-only property
or the object you're initializing is itself a collection you'll have to do
it the hard way.

It's not a bad idea to give a collection type a constructor that takes an
IEnumerable<of values to add, and an .AddRange() to do the same after
construction. Some (but not all) standard collection types have this. Using
this you *can* combine collection initialization and property assignment, if
not through an actual collection initializer.

--
J.
Oct 8 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
15
by: Tony Johansson | last post by:
Hello! Assume I have a class named Student and I instansiate an object called kasia in this way. Student kasia(100); What the difference if I instead instansiate this kasia in this way....
9
by: Jimmy Cerra | last post by:
I am a little confused how the memory for objects is allocated in JavaScript. David Flanagan, in "JavaScript: The Definitive Guide," states that each property of a class takes up memory space when...
8
by: Baloff | last post by:
Hello I am not sure why my compiler will not initialize string e1("sam"); and will initialize string e1 = "sam"; here is my code and the error. thanks alot
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
4
by: =?Utf-8?B?QWw=?= | last post by:
Hello, I'm writing a service using VS2005 and C#. I've found that only the objects used as parameters in WebMethods get exported to proxy dlls and the Service Description. I'd like my service...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
3
by: shapper | last post by:
Hello, I have the following loop and error message: foreach (Theme theme in Profile.Collaborator.Themes) { ... And I get the following error: Object reference not set to an instance of an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.