using ToString() in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Madmartigan
    New Member
    • Dec 2006
    • 23

    using ToString() in C#

    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.



    Code:
     using System;
     
     abstract public class Employee
     {
    	protected string name;
    	protected string idNumber;
    	protected int experience;
    	
    	public Employee()
    	{
    	}
    		
    	public Employee(string strName, string strIDNumber)
    	{
    		name 		= strName;
    		idNumber	= strIDNumber;
    	}
    	
    	public abstract string Evaluate();
    	
    	abstract public int Experience
    	{
    		get;
    		set;
    	}
     }
     
     public class Programmer : Employee
     {
     	int count = 0;
     	string PSenior = "Senior";
     	string PJunior = "Junior";
     	
    
    	public override int Experience
    	{
    		get
    		{
    			for (count = 0; count <= 10; count++);
    			return experience;
    		}
    		set
    		{
    			experience = value;
    			count = count + 1;
    		}
    	}
    	
      	public override string Evaluate()
     	{
     		if (experience > 5) {
     			return PSenior;
     		}
     		else {
     			return PJunior;
     		}
     	}
     	
     }
     
     public class Analyst : Employee
     {
     	int count = 0;
     	string ASenior = "Senior";
     	string AJunior = "Junior";
     	
     	
     	public override int Experience
    	{
    		get
    		{
    			for (count = 0; count <= 20; count++);
    			return experience;
    		}
    		set
    		{
    			experience = value;
    			count = count + 1;
    		}
    	}
    	
    	public override string Evaluate()
     	{
     		if (experience > 12) {
     			return ASenior;
     		}
     		else {
     			return AJunior;
     		}
     	}
    	
    
     }
     
     
     public class Test
     {
     	public static void Main()
     	{
     		Analyst myAnalyst = new Analyst("John Doe", "7402293945084", 5);   // I'd like to test these properties
     		
     		Console.WriteLine("phew, lets see my analysts name: ",
     		                  myAnalyst.name());
     		Console.WriteLine("his idNumber: ",
     		                  myAnalyst.idNumber());
     		Console.WriteLine("and the classification from the evaluate method: ",
     		                  myAnalyst.Evaluate());
     		Console.WriteLine("which was based on his experience level of: " + experience); 		
     	}
     }
    Last edited by kenobewan; Mar 9 '08, 01:10 PM. Reason: Deleted assignment question
  • Madmartigan
    New Member
    • Dec 2006
    • 23

    #2
    A further attempt whic I think is a bit neater but still no joy :(



    Code:
     using System;
     
     abstract public class Employee
     {
    	protected string name;
    	protected string idNumber;
    	protected int experience;
    	
    	public Employee()
    	{
    	}
    		
    	public Employee(string strName, string strIDNumber)
    	{
    		this.name 		= strName;
    		this.idNumber	= strIDNumber;
    	}
    	
    	public abstract string Evaluate();
    	
    	abstract public int Experience
    	{
    		get;
    		set;
    	}
    	
     }
     
     public class Programmer : Employee
     {
     	int count ;
     	string PSenior ;
     	string PJunior ;
     	
    
     	public Programmer()
     	{
     		count = 0;
     		PSenior = "Senior";
     		PJunior = "Junior";
     	}
     	
     	
     	public Programmer(string strName, string strIDNumber, int IntExperience) 	: base(strName, strIDNumber)
     	{
     		this.experience	= IntExperience;
     	}
        
     	public override int Experience
    	{
    		get
    		{
    			for (count = 0; count <= 10; count++);
    			return experience;
    		}
    		set
    		{
    			experience = value;
    			count = count + 1;
    		}
    	}
    	
      	public override string Evaluate()
     	{
     		if (experience > 5) {
      			return PSenior;
     		}
     		else {
      			return PJunior;
     		}
     	}
     	
      	public override string ToString()
      	{
      		return "["			+
      			   name			+
      			   ", " 		+
      			   idNumber     +
      			   ", "			+
      			   experience;  			   
      	}
    
     }
     
     public class Analyst : Employee
     {
     	int count = 0;
     	string ASenior ;
     	string AJunior ;
     	
     	public Analyst()
     	{
     		count = 0;
     		ASenior = "Senior";
     		AJunior = "Junior";
     	}
     	
     	public Analyst(string strName, string strIDNumber, int IntExperience)	: base(strName, strIDNumber)
     	{
     		this.experience = IntExperience;
     	}
     	
     	
     	public override int Experience
    	{
    		get
    		{
    			for (count = 0; count <= 20; count++);
    			return experience;
    		}
    		set
    		{
    			experience = value;
    			count = count + 1;
    		}
    	}
    	
    	public override string Evaluate()
     	{
     		if (experience > 12) {
     			return ASenior;
     		}
     		else {
     			return AJunior;
     		}
     	}
    	
    	public override string ToString()
      	{
      		return "["			+
      			   name			+         // is this how I write ToString method to display information
      			   ", " 		+
      			   idNumber     +
      			   ", "			+
      			   experience;  			   
      	}
    
    
     }
     
     
     public class Test
     {
     	public static void Main()
     	{
     		Programmer mp = new Programmer("John Doe", 
     		                                "7402293945084",
     		                                5
     		                                );   // I'd like to test these properties
     		
     		Console.WriteLine("Phew:", 
     		                  mp.ToString());    // or
     		Console.WriteLine("Still not",
     		                  mp.Evaluate());
     	}
     }

    Comment

    • enggwaqas
      New Member
      • Jun 2007
      • 19

      #3
      Regarding your ToString() overriding question, try this:


      Code:
      Console.WriteLine("Phew:{0}", 
       		                  mp.ToString());

      Comment

      Working...