big daddy

big daddy

  • NA
  • 1
  • 0

using clone to create an instance

Jan 30 2008 8:36 PM

I am trying to use clone for the first time but can't get it to work properly. I am going through the andy harris c# for the absolute beginner book and am really stuck on this one. I want to clone the Critter class and create an instance but it keeps erroring out.

 

using System;

namespace Critter {
  /// <summary>
  /// Critter
  /// Demonstrates basic OOP principles including properties and methods
  /// Andy Harris, 12/13/01
  /// </summary>
    class CritViewer
    {
        static void Main(string[] args)
        {
            //the main method simply creates an instance of the critviewer object
            CritViewer cv = new CritViewer();
        }//end main

        //This next method is the constructor for CritViewer
        public CritViewer()
        {
            Critter alpha = new Critter("alpha", 10, 10, 0);
            Critter beta = new Critter("beta");
            Critter charlie = new Critter("Charlie", 10);
            Clone myClone = new clone();
            myClone.name = "Dolly";

            Console.WriteLine(Dolly.talk());
            Console.WriteLine(alpha.talk());
            Console.WriteLine(beta.talk());
            Console.WriteLine(charlie.talk());

            Console.WriteLine();
            Console.WriteLine("Please press enter to continue");
            Console.ReadLine();
        }//end constructor


        public class clone : Critter
        {
        }//end clone
		
    static int showMenu(){
      int input = 1;
      Console.WriteLine();
      Console.WriteLine();
      Console.WriteLine();
      Console.WriteLine();
      Console.WriteLine("0) Exit");
      Console.WriteLine("1) Listen to Critter");
      Console.WriteLine("2) Feed Critter");
      Console.WriteLine("3) Play with Critter");
      Console.WriteLine("4) Rename Critter");
      try {
        input = Convert.ToInt32(Console.ReadLine());
      } catch (FormatException) {
        Console.WriteLine("Incorrect input");
      } // end try
      return input;
    } // end showMenu
  } // end class
	
  class Critter {
    private string pName;
    private int pFull = 10;
    private int pHappy = 10;
    private int pAge = 0;

    public Critter(string theName, int fullness) {
        name = theName;
        pFull = fullness;
    
    }//end constructor
      
    public Critter(string theName, int happiness, int theAge)
    {
        name = theName;
        pHappy = happiness;
        pAge = theAge;
    }

    public Critter(string theName)
    {
        name = theName;
    }//end constructor
    public Critter(string theName, int fullness, int happiness, int theAge)
    {
        name = theName;
        pFull = fullness;
        pHappy = happiness;
        pAge = theAge;
    }//end constructor
    
    public string name {
      get {
        return pName;
      } // end get
      set{
        if (value.Length > 8){
          Console.WriteLine("The name can't be more than 8 characters");
          pName = value;
          pName = pName.Substring(0,8);
          Console.WriteLine("Changing name to {0}", pName);
        } else {
          pName = value;
        } // end if
      } // end set
    } // end string property  

    public string talk(){
      string message;
      message = "The critter says: \n";
      
      if (pHappy > 5) {
        message += "  Hi!  My name is " + name + "\n";
        message += "  I feel happy today! \n";
      } else if (pHappy > 2) {
        message += "  " + name + " doesn't feel so good...";
      } else if (pHappy > 0) {
        message += "  " + name + " is MAD...";
      } else {
        message += " ...nothing at all, but lays in a heap.";
      } // end if
      return message;
    } // end talk

    public void age(){
      //handles aging the critter
      pAge++;
      pFull--;
      pHappy--;
      
      if (pFull < 3) {
        //if hungry, accellerate unhappiness
        pHappy--;
      } // end if
    } // end age
    
    public void play(){
      pHappy += 3;
    } // play
    
    public void eat(){
      pFull += 4;  
    } // end eat
          
  } // end class
} // end namespace