473,508 Members | 4,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using ToString() in C#

23 New Member
Hi

I have the following class to write, with respective methods and such(see below).
The script I have written so far is also attached. I am battling with two parts of this question. I'd like to include a test class to see whether this works but don't know how to override the protected fields so that I can pass properties to my analyst or programmer class.

Lastly, I have no idea how the ToString() method works to apply it to the script as the question asks : "Now override the ToString method to display the employee's information, e.g.: Programmer (Senior): John Doe (7402293945084)".

I have tried all sorts of different ways of doing this but it's kinda like trying to find a needle in a haystack. Any help would be appreciated.



Expand|Select|Wrap|Line Numbers
  1.  using System;
  2.  
  3.  abstract public class Employee
  4.  {
  5.     protected string name;
  6.     protected string idNumber;
  7.     protected int experience;
  8.  
  9.     public Employee()
  10.     {
  11.     }
  12.  
  13.     public Employee(string strName, string strIDNumber)
  14.     {
  15.         name         = strName;
  16.         idNumber    = strIDNumber;
  17.     }
  18.  
  19.     public abstract string Evaluate();
  20.  
  21.     abstract public int Experience
  22.     {
  23.         get;
  24.         set;
  25.     }
  26.  }
  27.  
  28.  public class Programmer : Employee
  29.  {
  30.      int count = 0;
  31.      string PSenior = "Senior";
  32.      string PJunior = "Junior";
  33.  
  34.  
  35.     public override int Experience
  36.     {
  37.         get
  38.         {
  39.             for (count = 0; count <= 10; count++);
  40.             return experience;
  41.         }
  42.         set
  43.         {
  44.             experience = value;
  45.             count = count + 1;
  46.         }
  47.     }
  48.  
  49.       public override string Evaluate()
  50.      {
  51.          if (experience > 5) {
  52.              return PSenior;
  53.          }
  54.          else {
  55.              return PJunior;
  56.          }
  57.      }
  58.  
  59.  }
  60.  
  61.  public class Analyst : Employee
  62.  {
  63.      int count = 0;
  64.      string ASenior = "Senior";
  65.      string AJunior = "Junior";
  66.  
  67.  
  68.      public override int Experience
  69.     {
  70.         get
  71.         {
  72.             for (count = 0; count <= 20; count++);
  73.             return experience;
  74.         }
  75.         set
  76.         {
  77.             experience = value;
  78.             count = count + 1;
  79.         }
  80.     }
  81.  
  82.     public override string Evaluate()
  83.      {
  84.          if (experience > 12) {
  85.              return ASenior;
  86.          }
  87.          else {
  88.              return AJunior;
  89.          }
  90.      }
  91.  
  92.  
  93.  }
  94.  
  95.  
  96.  public class Test
  97.  {
  98.      public static void Main()
  99.      {
  100.          Analyst myAnalyst = new Analyst("John Doe", "7402293945084", 5);   // I'd like to test these properties
  101.  
  102.          Console.WriteLine("phew, lets see my analysts name: ",
  103.                            myAnalyst.name());
  104.          Console.WriteLine("his idNumber: ",
  105.                            myAnalyst.idNumber());
  106.          Console.WriteLine("and the classification from the evaluate method: ",
  107.                            myAnalyst.Evaluate());
  108.          Console.WriteLine("which was based on his experience level of: " + experience);         
  109.      }
  110.  }
Mar 8 '08 #1
2 1230
Madmartigan
23 New Member
A further attempt whic I think is a bit neater but still no joy :(



Expand|Select|Wrap|Line Numbers
  1.  using System;
  2.  
  3.  abstract public class Employee
  4.  {
  5.     protected string name;
  6.     protected string idNumber;
  7.     protected int experience;
  8.  
  9.     public Employee()
  10.     {
  11.     }
  12.  
  13.     public Employee(string strName, string strIDNumber)
  14.     {
  15.         this.name         = strName;
  16.         this.idNumber    = strIDNumber;
  17.     }
  18.  
  19.     public abstract string Evaluate();
  20.  
  21.     abstract public int Experience
  22.     {
  23.         get;
  24.         set;
  25.     }
  26.  
  27.  }
  28.  
  29.  public class Programmer : Employee
  30.  {
  31.      int count ;
  32.      string PSenior ;
  33.      string PJunior ;
  34.  
  35.  
  36.      public Programmer()
  37.      {
  38.          count = 0;
  39.          PSenior = "Senior";
  40.          PJunior = "Junior";
  41.      }
  42.  
  43.  
  44.      public Programmer(string strName, string strIDNumber, int IntExperience)     : base(strName, strIDNumber)
  45.      {
  46.          this.experience    = IntExperience;
  47.      }
  48.  
  49.      public override int Experience
  50.     {
  51.         get
  52.         {
  53.             for (count = 0; count <= 10; count++);
  54.             return experience;
  55.         }
  56.         set
  57.         {
  58.             experience = value;
  59.             count = count + 1;
  60.         }
  61.     }
  62.  
  63.       public override string Evaluate()
  64.      {
  65.          if (experience > 5) {
  66.               return PSenior;
  67.          }
  68.          else {
  69.               return PJunior;
  70.          }
  71.      }
  72.  
  73.       public override string ToString()
  74.       {
  75.           return "["            +
  76.                  name            +
  77.                  ", "         +
  78.                  idNumber     +
  79.                  ", "            +
  80.                  experience;                 
  81.       }
  82.  
  83.  }
  84.  
  85.  public class Analyst : Employee
  86.  {
  87.      int count = 0;
  88.      string ASenior ;
  89.      string AJunior ;
  90.  
  91.      public Analyst()
  92.      {
  93.          count = 0;
  94.          ASenior = "Senior";
  95.          AJunior = "Junior";
  96.      }
  97.  
  98.      public Analyst(string strName, string strIDNumber, int IntExperience)    : base(strName, strIDNumber)
  99.      {
  100.          this.experience = IntExperience;
  101.      }
  102.  
  103.  
  104.      public override int Experience
  105.     {
  106.         get
  107.         {
  108.             for (count = 0; count <= 20; count++);
  109.             return experience;
  110.         }
  111.         set
  112.         {
  113.             experience = value;
  114.             count = count + 1;
  115.         }
  116.     }
  117.  
  118.     public override string Evaluate()
  119.      {
  120.          if (experience > 12) {
  121.              return ASenior;
  122.          }
  123.          else {
  124.              return AJunior;
  125.          }
  126.      }
  127.  
  128.     public override string ToString()
  129.       {
  130.           return "["            +
  131.                  name            +         // is this how I write ToString method to display information
  132.                  ", "         +
  133.                  idNumber     +
  134.                  ", "            +
  135.                  experience;                 
  136.       }
  137.  
  138.  
  139.  }
  140.  
  141.  
  142.  public class Test
  143.  {
  144.      public static void Main()
  145.      {
  146.          Programmer mp = new Programmer("John Doe", 
  147.                                          "7402293945084",
  148.                                          5
  149.                                          );   // I'd like to test these properties
  150.  
  151.          Console.WriteLine("Phew:", 
  152.                            mp.ToString());    // or
  153.          Console.WriteLine("Still not",
  154.                            mp.Evaluate());
  155.      }
  156.  }
Mar 8 '08 #2
enggwaqas
19 New Member
Regarding your ToString() overriding question, try this:


Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine("Phew:{0}", 
  2.                            mp.ToString());
Mar 9 '08 #3

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

Similar topics

0
6414
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
1
6393
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
2
13481
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
6
17140
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
3
3967
by: Babillon | last post by:
Hello all. I'm writting a little application to help me expand my knowledge in a few different areas. The app is, as you might of guessed from the subject, a system metrics display using WMI. For...
3
5143
by: mahajanvit | last post by:
Hi one and all I got this problem during my project. So in order to solve this I made a very small application. I am trying to insert using SP and sqldatasource control. I know that while using...
9
4657
by: Kevin Blount | last post by:
Here's the code I tried, and found it failed... <form runat="server" method="post" name="CreditCardForm" id="CreditCardForm"> <% foreach (object item in Request.Form) { if...
11
26920
PaullyB
by: PaullyB | last post by:
Hi There I'm trying to insert a CSV file into SQL Database using C#. I can read the csv file and insert it into the table, however any fields with an embebbed comma are not being read correctly. How...
5
6681
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
0
7228
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
7393
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
7502
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
5635
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,...
1
5057
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...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
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...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.