473,888 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[C#]proper inheritance/polymorphism design wanted

121 New Member
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
1 1620
mattmao
121 New Member
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

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

Similar topics

3
8398
by: Joe Delphi | last post by:
Does Visual Basic support multiple inheritance? That is one child class inheriting from more than one parent class. JD
37
2869
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
19
3200
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{ public: MyString(){} };
7
1503
by: DJ.precario | last post by:
In a schematic way, I have a program with a virtual base class (MyClass), and some derived classes (MyClass1, Myclass2...) In main, I want to create a vector of objects of the classes that derive from MyClass. My question is: -Can I have such a vector with actual objects of different kinds but all deriving from MyClass or does it have to be a vector of pointers to such objects?
14
12935
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
12
7075
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns I've ended up using successfully for certain kinds of inheritance and polymorphism, and some that have not worked out so well. Let's start with obvious things that turn out not to work well: 1. Use interface classes and "Implements" for...
15
330
by: scorpion53061 | last post by:
I am just now really starting to get into inheritance - controls, datasets, views all that stuff and I am seeing the enormous savings in performance and even watching the exe file size go down which is making me very happy. I am frustrated a bit though it seems to be costing me more time in that I am kind of not seeing the entire picture in the development environment. I was wondering how you more experienced folks handle this ......I...
8
1725
by: shuisheng | last post by:
Dear All, I have a libaray which provides a base class of name Base and several derived classes of name Derived1, Derived2 and so on. I want to add a client data into those classes, such as class MyBase: public Base { ClientDataType ClientData;
8
20621
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the fundanmental concept so i can remember it forever as it is a common interview question.... Thanks in advance
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4247
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.