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

Generics for Customer and Phones.... should be easy!

Hi,
I'm building an application where I've defined a custom class Customer.
Customer can have many phones (defined by phoneType and phoneNumber). I want
to check that a phoneNumber is not already present in customer phones. So
i've build a PhoneCollection class inheriting for List<Phoneand defined an
Add method to check if the phoneNumber already exists. Then I added the
PhoneCollection to Customer properties.
Everything works fine, and I can add new phones to customer using
cust.Phones.Add("1234") but Customer.Phones has Count=0 and if I look at it
in the debugger the phones are inside rawData (??). Strange thing is that if
I use customer.Phones[i] i got the i phone object!
What am I missing?? Below you can find the classes used.
Any help is welcome!
Bye,
Stefano

public class Customer
{
private String id;
private readonly PhoneCollection phones = new PhoneCollection();

public Customer(string id)
{
this.id = id;
}

public PhoneCollection Phones
{
get
{
return this.phones;
}
}
}

public class Phone
{
private string id;
private string ownerId;
private int phoneType;
private string phonenumber;

public Customer(string id)
{
this.id = id;
}

public int PhoneType
{
get{return phoneType;}
set{phoneType = value;}
}

public string PhoneNumber
{
get{return phoneNumber;}
set{phoneNumber = value;}
}
}

public class PhoneCollection: List<Phone>
{
private List<Phonem_Phones = new List<Phone>();

public PhoneCollection()
{
m_Phones.Capacity = 10;
}

public void Add(string phoneNumber)
{
//Here I add a new Phone to m_Phones if the phoneNumber is not present
}
}


Oct 9 '06 #1
5 1906
You are mixing inheritance and encapsulation; badly. Either you *are* a
list, or you *contain* the list. In your case, you do both. The methdos
you haven't overridden are pointing at the inherited list, where-as
..Add is looking at the contained list.

Basically, remove m_Phones completely; you *are* the list. Replace the
m_Phones.Something() methods to base.Something() - i.e. call the base
version of this method - i.e.

public void Add(string number) {
if(!Contains(number)) base.Add(number);
}

Although, personally I don't like this usage as people generally expect
..Add to either add or throw.

Marc

Oct 9 '06 #2
"Stefano Peduzzi" <pe***@interfree.ita écrit dans le message de news:
Ot**************@TK2MSFTNGP03.phx.gbl...

| public class PhoneCollection: List<Phone>
| {
| private List<Phonem_Phones = new List<Phone>();

If you are inheiting from List<Phone>, then you do not need to also have an
internal list.

| public PhoneCollection()
| {
| m_Phones.Capacity = 10;
| }
|
| public void Add(string phoneNumber)
| {
| //Here I add a new Phone to m_Phones if the phoneNumber is not present
| }
| }

The problem is that you are inheriting from a class and then using an inner
list to store the numbers rather than the instance of the PhoneCollection
class, which is a list in itself. Also, declaring your own Add method will
hide the original Add method and should have given you a warning.

If I were you I would restructure this to declare your own class that no
longer inherits, but that simply contains a list, or better still a
Dictionary<K,V>.

public class PhoneCollection
{
private Dictionary<string, Phonephones; = new Dictionary<string,
Phone>();

public void Add(string phoneNumber)
{
if (!phones.ContainsKey(phoneNumber))
phones.Add(phoneNumber, new Phone(...));
}

public Phone this[string number]
{
get { return phones[number]; }
}

public Phone this[int index]
{
get { return phones.Values[index]; }
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 9 '06 #3
Hi Joanna,
Thanks for your help! I tried the "route" you proposed and it is really
interesting (it works with 5 lines of code!). I've found 2 problems with it:
- public Phone this[int index]{ get { return phones.Values[index]; }}
gives me this compiler error:
Cannot apply indexing with [] to an expression of type
'System.Collections.Generic.Dictionary<string,Busi nessEntity.Phone>.ValueCollection'
C:\Progetti\Test\BusinessEntity\PhoneCollection2.c s 28 18 BusinessEntity
-If I write
foreach (Phone tmp in c.Phones2)
I get this compiler error:
Error 1 foreach statement cannot operate on variables of type
'BusinessEntity.PhoneCollection2' because 'BusinessEntity.PhoneCollection2'
does not contain a public definition for 'GetEnumerator'
C:\Progetti\Test\Test\Form1.cs 97 13 Test

As you can see... if you have some good link on Dictionary it would be
useful!
Thanks,
Stefano
"Joanna Carter [TeamB]" <jo****@not.for.spamha scritto nel messaggio
news:eb****************@TK2MSFTNGP04.phx.gbl...
"Stefano Peduzzi" <pe***@interfree.ita écrit dans le message de news:
Ot**************@TK2MSFTNGP03.phx.gbl...

| public class PhoneCollection: List<Phone>
| {
| private List<Phonem_Phones = new List<Phone>();

If you are inheiting from List<Phone>, then you do not need to also have
an
internal list.

| public PhoneCollection()
| {
| m_Phones.Capacity = 10;
| }
|
| public void Add(string phoneNumber)
| {
| //Here I add a new Phone to m_Phones if the phoneNumber is not present
| }
| }

The problem is that you are inheriting from a class and then using an
inner
list to store the numbers rather than the instance of the PhoneCollection
class, which is a list in itself. Also, declaring your own Add method will
hide the original Add method and should have given you a warning.

If I were you I would restructure this to declare your own class that no
longer inherits, but that simply contains a list, or better still a
Dictionary<K,V>.

public class PhoneCollection
{
private Dictionary<string, Phonephones; = new Dictionary<string,
Phone>();

public void Add(string phoneNumber)
{
if (!phones.ContainsKey(phoneNumber))
phones.Add(phoneNumber, new Phone(...));
}

public Phone this[string number]
{
get { return phones[number]; }
}

public Phone this[int index]
{
get { return phones.Values[index]; }
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Oct 9 '06 #4
Hi Marc,
As for Joanna... thanks for your help! Also your hints worked, so I removed
m_Phones. I have now a problem with Contains that always returns true. I've
implemented the Phone class inheriting from IComparable<Phoneand I defined
Equals and CompareTo.. I think (...) it is not enough: what should I
implement to have Contains working?

Thanks,
Stefano
"Marc Gravell" <ma**********@gmail.comha scritto nel messaggio
news:11**********************@i3g2000cwc.googlegro ups.com...
You are mixing inheritance and encapsulation; badly. Either you *are* a
list, or you *contain* the list. In your case, you do both. The methdos
you haven't overridden are pointing at the inherited list, where-as
.Add is looking at the contained list.

Basically, remove m_Phones completely; you *are* the list. Replace the
m_Phones.Something() methods to base.Something() - i.e. call the base
version of this method - i.e.

public void Add(string number) {
if(!Contains(number)) base.Add(number);
}

Although, personally I don't like this usage as people generally expect
.Add to either add or throw.

Marc

Oct 9 '06 #5
"Stefano Peduzzi" <pe***@interfree.ita écrit dans le message de news:
%2***************@TK2MSFTNGP05.phx.gbl...

| Thanks for your help! I tried the "route" you proposed and it is really
| interesting (it works with 5 lines of code!). I've found 2 problems with
it:
| - public Phone this[int index]{ get { return phones.Values[index]; }}
| gives me this compiler error:
| Cannot apply indexing with [] to an expression of type
|
'System.Collections.Generic.Dictionary<string,Busi nessEntity.Phone>.ValueCollection'

Sorry, my bad, I was mixing List<Tcode :-) Do you really need an integer
index ?

| C:\Progetti\Test\BusinessEntity\PhoneCollection2.c s 28 18 BusinessEntity
| -If I write
| foreach (Phone tmp in c.Phones2)
| I get this compiler error:
| Error 1 foreach statement cannot operate on variables of type
| 'BusinessEntity.PhoneCollection2' because
'BusinessEntity.PhoneCollection2'
| does not contain a public definition for 'GetEnumerator'
| C:\Progetti\Test\Test\Form1.cs 97 13 Test

Then add the IEnumerable<Phoneinterface to PhoneCollection and wire it to
the dictionary.

public class PhoneCollection : IEnumerable<Phone>
{
IEnumerator<PhoneGetEnumerator()
{
return phones.Values.GetEnumerator();
}
}

| As you can see... if you have some good link on Dictionary it would be
| useful!

Take a look at the examples in the help.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 9 '06 #6

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

Similar topics

3
by: Thomas Weholt | last post by:
Hi, Is it at all possible to use python to make apps on cellular/mobile phones, using something like Jython etc. ? Hm ... ?? Thomas
11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
6
by: russ | last post by:
Hi, We have stumbled across an issue using the type safe collection System.Collections.ObjectModel.Collection <T> to retrieve data from our data layer. Say we have a customer object and want...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
10
by: Frank Rizzo | last post by:
Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I am converting them to List<and Dictionary<respectively. It's a pretty massive system, so there are a lot of casts. For...
4
by: Random | last post by:
I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be...
1
by: =?Utf-8?B?YmlsbHI=?= | last post by:
Sorry if this is the wrong group, but I cna think not of where else to post. I have an inheritance hierarchy in place as follows ... PersonVisitor<T<-- AlphabeticVisitor<T<--...
4
by: Bruno Neves Pires Silva | last post by:
Hello, Programmers. How can I access a member of an object using generics? I've got the following problem:I Have a class that uses generics like below: class ClassName<typename> { public...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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,...

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.