By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

[C#]proper inheritance/polymorphism design wanted

100+
P: 121
I am brand new to C#.NET so here is my trial on this lab exercise:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5.  
  6. namespace lab02exec
  7. {
  8.     public class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ArrayList myContacts = new ArrayList();
  13.  
  14.             //switching user activities:
  15.             do
  16.             {
  17.                 //prompt introductions:
  18.                 Console.WriteLine("Please choose what you gonna do?");
  19.                 Console.WriteLine("a - Add a new contact to the list");
  20.                 Console.WriteLine("d - Delete a contact from the list");
  21.                 Console.WriteLine("e - Edit a contact in the list");
  22.                 Console.WriteLine("i - Display a contact in the list");
  23.                 Console.WriteLine("l - List all contacts in the list");
  24.                 Console.WriteLine("q - Quit from this program");
  25.  
  26.                 //grab user's choice,for keeping it simple,
  27.                 //no input validation would be carried out..
  28.                 string input = Console.ReadLine();
  29.                 switch (input)
  30.                 {
  31.                     case "a":
  32.                         AddContact(myContacts);
  33.                         break;
  34.                     //stub: omit other options.
  35.                     default:
  36.                         break;
  37.                 }
  38.  
  39.             }
  40.             while (true);
  41.  
  42.  
  43.         }
  44.  
  45.         public static void AddContact(ArrayList list)
  46.         {
  47.             Console.WriteLine("Please choose which option to add:");
  48.             Console.WriteLine("p - Adding a Personal contact");
  49.             Console.WriteLine("b - Adding a Business contact");
  50.  
  51.             string input = Console.ReadLine();
  52.             switch (input)
  53.             {
  54.                 case "p":
  55.                     AddPersonalContact(list);
  56.                     break;
  57.                 case "b":
  58.                     AddBusinessContact(list);
  59.                     break;
  60.                 default:
  61.                     break;
  62.             }
  63.  
  64.         }
  65.  
  66.         public static void AddPersonalContact(ArrayList list)
  67.         {
  68.             Console.WriteLine("Input syntax: \"name, address, phonenumber\"");
  69.  
  70.             //split the input string into substrings.
  71.             string input = Console.ReadLine();
  72.             string[] data = new string[3];
  73.             char[] splitter = { ',' };
  74.             data = input.Split(splitter);
  75.  
  76.             //instantiate a new PersonalContact and put it into the ArrayList.
  77.             PersonalContact pc = new PersonalContact(list, data);
  78.         }
  79.  
  80.         public static void AddBusinessContact(ArrayList list)
  81.         {
  82.             Console.WriteLine("Input syntax: \"name, company, phonenumber, faxnumber\"");
  83.  
  84.             //split the input string into substrings.
  85.             string input = Console.ReadLine();
  86.             string[] data = new string[4];
  87.             char[] splitter = { ',' };
  88.             data = input.Split(splitter);
  89.  
  90.             BusinessContact bc = new BusinessContact(list, data);
  91.         }
  92.     }
  93.  
  94.     //the base contact class 
  95.     public abstract class Contact
  96.     {
  97.         //stub
  98.     }
  99.  
  100.     //Applying inheritance and polymorphism:
  101.     public class PersonalContact : Contact
  102.     {
  103.         struct PersonalData
  104.         {
  105.             //for keeping it simple, just use public fields in this struct.
  106.             //though accessor methods would be the normal choice.
  107.             public string name;
  108.             public string address;
  109.             public string phonenumber;
  110.         }
  111.  
  112.         public PersonalContact(ArrayList list, string[] data)
  113.         {
  114.             //Use a struct to store personal data.
  115.             PersonalData newRecord;
  116.  
  117.             newRecord.name = data[0];
  118.             newRecord.address = data[1];
  119.             newRecord.phonenumber = data[2];
  120.  
  121.             //put the struct into an ArrayList.
  122.             list.Add(newRecord);
  123.         }
  124.     }
  125.  
  126.     public class BusinessContact : Contact
  127.     {
  128.         struct BusinessData
  129.         {
  130.             //for keeping it simple, just use public fields in this struct.
  131.             public string name;
  132.             public string company;
  133.             public string phonenumber;
  134.             public string faxnumber;
  135.         }
  136.  
  137.         public BusinessContact(ArrayList list, string[] data)
  138.         {
  139.             //Use a struct to store personal data.
  140.             BusinessData newRecord;
  141.  
  142.             newRecord.name = data[0];
  143.             newRecord.company = data[1];
  144.             newRecord.phonenumber = data[2];
  145.             newRecord.faxnumber = data[3];
  146.  
  147.             //put the struct into an ArrayList.
  148.             list.Add(newRecord);
  149.         }
  150.     }
  151. }
If you take a look at it, you would know that I was trying to implement the inheritance/polymorphism by making an abstruct base class and two derived classes from it.

My code compiles and runs, I reckon both of PersonalData and BusinessData struct typed data can be successfully addes into the ArrayList as I want, so the "Adding a new contact" functionality is done.

But I don't know my way of doing it is correct, anyway, this abstruct base class doesn't do anying at all but only provides an "interface" to its two derived classes.

If mine is wrong, then what would be the correct design?

I just need some informative instructions and I think I am able to implement ithe rest of this program shortly.

BTW I am not sure about this: Can you tell from outside of a ArrayList which would be the name field of the struct in the ArrayList?

Say, In the ArrayList you got a PersonalData struct pd, of which name is John. Can you issue some command like:

string name = pd->name; (This is what I did in ANSI C)

in C#?

If not, then how would you tell which is the one you want by its name filed if there are many sturct variables store inside the ArrayList?
Mar 7 '08 #1
Share this Question
Share on Google+
1 Reply


100+
P: 121
After several hours' work I change my design a little bit. Now it successfully demonstrates the Adding, Deleting and Listing functionalities:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5.  
  6. namespace lab02exec
  7. {
  8.     public class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ArrayList myContacts = new ArrayList();
  13.             string input;
  14.  
  15.             //switching user activities:
  16.             do
  17.             {
  18.                 //prompt introductions:
  19.                 Console.WriteLine("Please choose what you gonna do?");
  20.                 Console.WriteLine("a - Add a new contact to the list");
  21.                 Console.WriteLine("d - Delete a contact from the list");
  22.                 Console.WriteLine("e - Edit a contact in the list");
  23.                 Console.WriteLine("i - Display a contact in the list");
  24.                 Console.WriteLine("l - List all contacts in the list");
  25.                 Console.WriteLine("q - Quit from this program");
  26.  
  27.                 //grab user's choice,for keeping it simple,
  28.                 //no input validation would be carried out..
  29.                 input = Console.ReadLine();
  30.                 switch (input)
  31.                 {
  32.                     case "a":
  33.                         AddContact(myContacts);
  34.                         break;
  35.                     //stub: omit other options.
  36.                     case "d":
  37.                         Console.WriteLine("Tell me the Personal/Business name to be deleted:");
  38.                         string name = Console.ReadLine();
  39.                         DeleteContact(myContacts, name);
  40.                         break;
  41.                     case "l":
  42.                         ListContacts(myContacts);
  43.                         break;
  44.                     case "q":
  45.                         break;
  46.                     default:
  47.                         break;
  48.                 }
  49.  
  50.             }
  51.             while (input!="q");
  52.  
  53.         }
  54.  
  55.         public static void AddContact(ArrayList list)
  56.         {
  57.             Console.WriteLine("Please choose which option to add:");
  58.             Console.WriteLine("p - Adding a Personal contact");
  59.             Console.WriteLine("b - Adding a Business contact");
  60.  
  61.             string input = Console.ReadLine();
  62.             switch (input)
  63.             {
  64.                 case "p":
  65.                     AddPersonalContact(list);
  66.                     break;
  67.                 case "b":
  68.                     AddBusinessContact(list);
  69.                     break;
  70.                 default:
  71.                     break;
  72.             }
  73.  
  74.         }
  75.  
  76.         public static void AddPersonalContact(ArrayList list)
  77.         {
  78.             Console.WriteLine("Input syntax: \"name, address, phonenumber\"");
  79.  
  80.             //split the input string into substrings.
  81.             string input = Console.ReadLine();
  82.             string[] data = new string[3];
  83.             char[] splitter = { ',' };
  84.             data = input.Split(splitter);
  85.  
  86.             PersonalContact pc = new PersonalContact(data);
  87.             list.Add(pc);
  88.         }
  89.  
  90.         public static void AddBusinessContact(ArrayList list)
  91.         {
  92.             Console.WriteLine("Input syntax: \"name, company, phonenumber, faxnumber\"");
  93.  
  94.             string input = Console.ReadLine();
  95.             string[] data = new string[4];
  96.             char[] splitter = { ',' };
  97.             data = input.Split(splitter);
  98.  
  99.             BusinessContact bc = new BusinessContact(data);
  100.             list.Add(bc);
  101.         }
  102.  
  103.         public static void ListContacts(ArrayList list)
  104.         {
  105.             for (int i = 0; i < list.Count; i++)
  106.             {
  107.                 //To differentiate PersonalContact from BusinessContact.
  108.                 if (list[i].ToString() == "lab02exec.PersonalContact")
  109.                 {
  110.                     Console.WriteLine("The {0} item in the contact list would be:", i+1);
  111.                     DisplayPersonalContact((PersonalContact)list[i]);
  112.                 }
  113.                 else
  114.                 {
  115.                     Console.WriteLine("The {0} item in the contact list would be:", i+1);
  116.                     DisplayBusinessContact((BusinessContact)list[i]);
  117.                 }
  118.             }
  119.         }
  120.  
  121.         public static void DisplayPersonalContact(PersonalContact data)
  122.         {
  123.             Console.WriteLine("Name: {0}", data.Name);
  124.             Console.WriteLine("Address: {0}", data.Address);
  125.             Console.WriteLine("Phone Number: {0}", data.PhoneNumber);
  126.         }
  127.  
  128.         public static void DisplayBusinessContact(BusinessContact data)
  129.         {
  130.             Console.WriteLine("Name: {0}", data.Name);
  131.             Console.WriteLine("Company: {0}", data.Company);
  132.             Console.WriteLine("Phone Number: {0}", data.PhoneNumber);
  133.             Console.WriteLine("Fax Number: {0}", data.Faxnumber);
  134.         }
  135.  
  136.         public static void DeleteContact(ArrayList list, string name)
  137.         {
  138.             for (int i = 0; i < list.Count; i++)
  139.             {
  140.                 if (list[i].ToString() == "lab02exec.PersonalContact")
  141.                 {
  142.                     if (((PersonalContact)list[i]).Name == name)
  143.                     {
  144.                         list.RemoveAt(i);
  145.                         break;
  146.                     }
  147.                 }
  148.                 else
  149.                 {
  150.                     if (((BusinessContact)list[i]).Name == name)
  151.                     {
  152.                         list.RemoveAt(i);
  153.                         break;
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     //the base contact class 
  161.     public abstract class Contact
  162.     {
  163.         //stub
  164.         public Contact()
  165.         {
  166.             //the default empty constructor.
  167.         }
  168.     }
  169.  
  170.     //Applying inheritance and polymorphism:
  171.     public class PersonalContact : Contact
  172.     {
  173.         private string name, address, phonenumber;
  174.  
  175.         public PersonalContact(string[] data) : base ()
  176.         {
  177.             name = data[0];
  178.             address = data[1];
  179.             phonenumber = data[2];
  180.         }
  181.  
  182.         //accessor methods.
  183.         public string Name
  184.         {
  185.             get { return name; }//read only
  186.         }
  187.  
  188.         public string Address
  189.         {
  190.             get { return address; }//read only
  191.         }
  192.  
  193.         public string PhoneNumber
  194.         {
  195.             get { return phonenumber; }//read only
  196.         }
  197.     }
  198.  
  199.     public class BusinessContact : Contact
  200.     {
  201.         private string name, company, phonenumber, faxnumber;
  202.  
  203.         public BusinessContact(string[] data)
  204.             : base()
  205.         {
  206.             name = data[0];
  207.             company = data[1];
  208.             phonenumber = data[2];
  209.             faxnumber = data[3];
  210.         }
  211.  
  212.         //accessor methods.
  213.         public string Name
  214.         {
  215.             get { return name; }//read only
  216.         }
  217.  
  218.         public string Company
  219.         {
  220.             get { return company; }//read only
  221.         }
  222.  
  223.         public string PhoneNumber
  224.         {
  225.             get { return phonenumber; }//read only
  226.         }
  227.  
  228.         public string Faxnumber
  229.         {
  230.             get { return faxnumber; } //read only
  231.         }
  232.     }
  233. }
But still I am unable to implement the inheritance/polymorphism well... And I realise that having two different classes in one ArrayList is painful...

Maybe it would be better if both of the PersonslContact and BusinesContact class can be treated as one base type? I get stuck at this point...
Mar 7 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.