From: Jeff Johnson on
"GiJeet" <gijeet(a)yahoo.com> wrote in message
news:8d5dfefe-98e3-4555-a160-d039445c3dd5(a)i9g2000yqi.googlegroups.com...

>I was looking for properties using get/set. And how to assign the
> list via the constructor?
>
> Example:
>
> //trying to create a list that's a property of a class holding IP
> addresses of a computer
> private List<string> lIPAddressList;
> internal List<string> IPAddressList {
> get { return lIPAddressList; }
> set { lIPAddressList = value; }
> }
>
>
> //The constructor of the class
> internal CustomException( ) {
> this.sUserName = Environment.UserName;
> this.sDomainName = Environment.UserDomainName;
> this.sComputerName = Dns.GetHostName();
>
> // what to assign the whole list to a property
> //this.lIPAddress =
> Dns.GetHostEntry(ComputerName).AddressList; //what to put here?
>
> }
>
> It's just a lit of strings. Could be an array or List<string> or
> whatever, just need to assign to a collection.

Assuming that the AddressList property above returns some enumerable
collection of strings you can use the List<> constructor that takes an
IEnumerable object:

IPAddressList = new
List<string>(Dns.GetHostEntry(ComputerName).AddressList);

(And yes, you can stick "this." onto the front of your property; that's just
not my preferred style.)


From: Family Tree Mike on
On 5/3/2010 2:27 PM, GiJeet wrote:
> I was looking for properties using get/set. And how to assign the
> list via the constructor?
>
> Example:
>
> //trying to create a list that's a property of a class holding IP
> addresses of a computer
> private List<string> lIPAddressList;
> internal List<string> IPAddressList {
> get { return lIPAddressList; }
> set { lIPAddressList = value; }
> }
>
>
> //The constructor of the class
> internal CustomException( ) {
> this.sUserName = Environment.UserName;
> this.sDomainName = Environment.UserDomainName;
> this.sComputerName = Dns.GetHostName();
>
> // what to assign the whole list to a property
> //this.lIPAddress =
> Dns.GetHostEntry(ComputerName).AddressList; //what to put here?
>
> }
>
> It's just a lit of strings. Could be an array or List<string> or
> whatever, just need to assign to a collection.

As the others have said, assuming AddressList is compatible, then your
code would be fine. I want to point out two and a half things though...

1. Don't refer to lIPAddressList in your code. Use the property. At
some time later if your setter method does other things, such as raise
an event, you need not change anything then.
1.A. If you don't do anything special in the property, you can use the
property in this form:

internal List<string> IPAddressList {get; set;}

which helps prevent using the backing field directly.
2. You can always just initialize the list using: IPAddressList = new
List<string>.


--
Mike
From: Arne Vajhøj on
On 03-05-2010 13:09, GiJeet wrote:
> Hello, is it possible to have a List<whatever> as a property of a
> class or must you use indexers? If so, please show me an example of
> the syntax.

As other have explained already then List<X> as type of
property is similar to all other types.

I will add that you should consider your object model
careful.

A property with both get and set of type List<X> is
not good encapsulation.

No set and a get that return a readonly List is fine.

..NET has the AsReadOnly method to return such a beast.

Arne