Exposing List<> outside class [modified]
-
Say i had this class:
public sealed class Partner
{
string something;
string somethingelse;
List<Customer> customers = new List<Customer>();public List<Customer> Customers { get { return customers; } }
}
Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use
class Partner : List<Customer>
but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....modified on Wednesday, November 26, 2008 8:22 AM
-
Say i had this class:
public sealed class Partner
{
string something;
string somethingelse;
List<Customer> customers = new List<Customer>();public List<Customer> Customers { get { return customers; } }
}
Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use
class Partner : List<Customer>
but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....modified on Wednesday, November 26, 2008 8:22 AM
-
I think your generics got swallowed by the HTML monster! :)
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))You're right - should be working now.
-
Say i had this class:
public sealed class Partner
{
string something;
string somethingelse;
List<Customer> customers = new List<Customer>();public List<Customer> Customers { get { return customers; } }
}
Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use
class Partner : List<Customer>
but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....modified on Wednesday, November 26, 2008 8:22 AM
Depending on the usage, Customers could be
IEnumerable<Customer>
. But I do not see any other way of mutating that list.Jan Sommer wrote:
I'm asking because my teacher says that the above example is bad OOP
So did he not bother to explain to you why he feels that way? Sounds like a bad teacher to me. Ask him if he is happy with what he does ;P
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Depending on the usage, Customers could be
IEnumerable<Customer>
. But I do not see any other way of mutating that list.Jan Sommer wrote:
I'm asking because my teacher says that the above example is bad OOP
So did he not bother to explain to you why he feels that way? Sounds like a bad teacher to me. Ask him if he is happy with what he does ;P
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))He said he didn't know a better way :P The first solution i came up with was to add this method to class Partner:
public IEnumerator<Customer> GetCustomersEnumerator()
{
return this.cust.GetEnumerator();
}which is working just fine for retrieving all the customers with this code:
IEnumerator<Customer> alist = partners.GetCustomerEnumerator();
while(alist.MoveNext())
{
Console.WriteLine(alist.Current.someProperty);
}But in the userinterface, when a user clicks the customer (shown in a listview) i'd like to use the customer-index to retrieve the correct customer and send him to another dialog. Currently i'm only able to access each customers information and i can't pick a specific customer, since i don't have access to the list. Am i doing it all wrong? I was considering using a database which would make everything alot easier, but that would be too hard for the other students (according to my teacher :P)
-
He said he didn't know a better way :P The first solution i came up with was to add this method to class Partner:
public IEnumerator<Customer> GetCustomersEnumerator()
{
return this.cust.GetEnumerator();
}which is working just fine for retrieving all the customers with this code:
IEnumerator<Customer> alist = partners.GetCustomerEnumerator();
while(alist.MoveNext())
{
Console.WriteLine(alist.Current.someProperty);
}But in the userinterface, when a user clicks the customer (shown in a listview) i'd like to use the customer-index to retrieve the correct customer and send him to another dialog. Currently i'm only able to access each customers information and i can't pick a specific customer, since i don't have access to the list. Am i doing it all wrong? I was considering using a database which would make everything alot easier, but that would be too hard for the other students (according to my teacher :P)
Jan Sommer wrote:
Am i doing it all wrong?
You have 2 choices: 1. Expose it as an
IList<Customer>
and docust.ToArray()
. 2. Wrap cust in aReadOnlyCollection<Customer>
. I would prefer the latter.xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Say i had this class:
public sealed class Partner
{
string something;
string somethingelse;
List<Customer> customers = new List<Customer>();public List<Customer> Customers { get { return customers; } }
}
Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use
class Partner : List<Customer>
but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....modified on Wednesday, November 26, 2008 8:22 AM
Why for heavens sake should this be bad coding practice?? This is the normal way to do it: Declaring a private field and exposing it through a public property. There's no other way to do it right - your teacher is definitely wrong! There is one concern with this I can think of: 1. The list should be immutable. If so, simply use
customers.AsReadOnly()
in your get accessor and provide some methods likeAdd/RemoveCustomer
in yourPartner
class. Regards Thomaswww.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software. -
Jan Sommer wrote:
Am i doing it all wrong?
You have 2 choices: 1. Expose it as an
IList<Customer>
and docust.ToArray()
. 2. Wrap cust in aReadOnlyCollection<Customer>
. I would prefer the latter.xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))Thanks... Didn't know of the ReadOnlyCollection but that seems reasonable to use..
-
Why for heavens sake should this be bad coding practice?? This is the normal way to do it: Declaring a private field and exposing it through a public property. There's no other way to do it right - your teacher is definitely wrong! There is one concern with this I can think of: 1. The list should be immutable. If so, simply use
customers.AsReadOnly()
in your get accessor and provide some methods likeAdd/RemoveCustomer
in yourPartner
class. Regards Thomaswww.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.I tend to agree. The only design guideline this might violate is from Bill Wagner's "Effective C#, Item 23: "Avoid Returning References to Internal Class Objects". But Thomas' suggestion to return a read-only object addresses (no pun intended) this issue.
-
Why for heavens sake should this be bad coding practice?? This is the normal way to do it: Declaring a private field and exposing it through a public property. There's no other way to do it right - your teacher is definitely wrong! There is one concern with this I can think of: 1. The list should be immutable. If so, simply use
customers.AsReadOnly()
in your get accessor and provide some methods likeAdd/RemoveCustomer
in yourPartner
class. Regards Thomaswww.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.i'm glad to hear i'm not completely wrong.. this sure have saved me a headache..
-
i'm glad to hear i'm not completely wrong.. this sure have saved me a headache..
Always at your service... :) Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.