Hi,
I am creating an address book example. For the purpose i have created
2 classes
one is
ContactInfo
name
department
PhoneInfo
OfficePhone
Mobile
Now I want to design the class in such a way that when I type
ContactInfo.PhoneInfo.mobile
it should take the list of mobile numbers
im using dotnet 2.0 and know little about generics.
can we implement generics for the same.
Jon Skeet [C# MVP] - 20 Jul 2007 08:35 GMT
> I am creating an address book example. For the purpose i have created
> 2 classes
[quoted text clipped - 13 lines]
> im using dotnet 2.0 and know little about generics.
> can we implement generics for the same.
I don't see where generics would come into it at all.
A few things aren't clear:
1) Is there any inheritance here?
2) Is PhoneInfo a nested class?
3) When you talk about typing ContactInfo.PhoneInfo.mobile, do you
mean in Visual Studio?
It sounds to me like your ContactInfo ought to have a reference to an
instance of PhoneInfo...
Jon
Adrian Voicu - 20 Jul 2007 13:40 GMT
You should add the a PhoneInfo object to the ContactInfo class and make a
property for it that can be called by other objects. You should also make a
property in the PhoneInfo class that returns the mobile phone number.
Similary you can addd more properties as you see fit, but the code below
allows you to get the mobile phone number of a contact like this:
ContactInfo myContactInfo = new ContactInfo(...); //depending on how you set
up your constructor
string mobileNum = myContactInfo.PhoneData.Mobile;
-------------------------------------------------------------------------
class ContactInfo
{
string m_name;
string m_department;
PhoneInfo m_phoneData;
public PhoneInfo PhoneData
{
get { return m_phoneData; }
}
....
}
class PhoneInfo
{
string m_officeNum;
string m_mobileNum;
public string Mobile
{
get { return m_mobileNum; }
}
....
}
Adrian.

Signature
[Please mark my answer if it was helpful to you]
> Hi,
> I am creating an address book example. For the purpose i have created
[quoted text clipped - 14 lines]
> im using dotnet 2.0 and know little about generics.
> can we implement generics for the same.