473,386 Members | 1,745 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,386 software developers and data experts.

Array of Class Question

jm
I'm looking at this code at MSDN:

http://msdn2.microsoft.com/en-us/lib...numerable.aspx

using System;
using System.Collections;

public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}

public string firstName;
public string lastName;
}

public class People : IEnumerable
{
private Person[] _people; //trying to understand this line
....

I wanted to know how they "all of a sudden" were able to declare the
variable _people as an array of Person[] user defined types. I didn't
see anything that would allow Person to be used as an array like this.
Probably something basic I forgot somewhere, but I can't find the
answer.

Thank you.

Nov 22 '06 #1
2 2810
You can declare an array of any type in C#:
int[] or Person[] , they are both instances of System.Array.
I think you are searching for problems in the place they are not present in
:-)

"jm" <ne***********@gmail.comwrote in message
news:11*********************@h54g2000cwb.googlegro ups.com...
I'm looking at this code at MSDN:

http://msdn2.microsoft.com/en-us/lib...numerable.aspx

using System;
using System.Collections;

public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}

public string firstName;
public string lastName;
}

public class People : IEnumerable
{
private Person[] _people; //trying to understand this line
...

I wanted to know how they "all of a sudden" were able to declare the
variable _people as an array of Person[] user defined types. I didn't
see anything that would allow Person to be used as an array like this.
Probably something basic I forgot somewhere, but I can't find the
answer.

Thank you.

Nov 22 '06 #2
Arrays are supported out-of-the-box for any type. There is no
distinction between user-defined an MS-defined. In fact SomeType[]
could really be considered as a 1.1 precursor to generics.

Note that the line in question doesn't *allocate* an array - just says
that _people *is* an array; if you added

_peope = new Person[10];

then it would allocate enough space for 10 Person references (it Person
was a struct it would allocate the space for the actual instances).
This *still* doesn't get you any people (they are all null references),
so you then need:

for(int i = 0; i < 10; i++) {
_people[i] = new Person();
}

et voila; 10 people; you can then access _people[0] thru _people[9]

Arrays in this usage are a fundamental concept provided by the runtime
/ compiler in most languages without having to involve the targetted
class. Suggest you read up on Array on MSDN...

Marc

Nov 22 '06 #3

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

Similar topics

6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
10
by: amparikh | last post by:
Ok, my question is not about Virtual destructors and why, but more on the significance. Generally we have a virtual destructor in the base class ( and inadvertently in the derived class) so that...
5
by: Abraham Lopez | last post by:
Hi.. Is there a way to convert a System.Array to XML... If you know thanks very much... if you don't... Please do not respond stupid things like " Yes -- many ways."
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
5
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
10
by: ALKASER266 | last post by:
Hey guyz I have a prac and I am beginner and I did this code> Is my code is complete and if is it not complete how i can complete it? and how i can arrange it more? How I can make my driver to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.