I'm supposed to make my class cloneable for class, but I can't get it to work... Here's what I have so far:

public class Employeex : ICloneable
    {
        // using properties instead of the constructor
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public int CurrentLocationID { get; set; }
        public object Clone()
        {
            Employeex copy = new Employeex();
            copy.EmployeeID = this.EmployeeID.Clone();
            copy.Name = this.Name.Clone();
            copy.CurrentLocationID = this.CurrentLocationID.Clone();
            return copy;

        }

    }

And here is the code my professor gave us as a reference to use:

public class clsPendingCH : ICloneable
{
public double[] Samples;
public object Clone()
{
clsPendingCH copy = new clsPendingCH();
copy.Samples = (double[])this.Samples.Clone();
return copy;
}
}

I get an error saying 'int' does not contain a definition for 'Clone', and cannot convert type 'object' to 'string'. Anyone have any ideas?

Which code are you trying to make work -- what you have or what your professor gave you?

public sealed class Employeex : ICloneable
  {
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public int CurrentLocationID { get; set; }
    public Employeex()
    {
    }
    #region ICloneable Members
    object ICloneable.Clone()
    {
      return this.MemberwiseClone();
    }
    public Employeex Clone()
    {
      return (Employeex)((ICloneable)this).Clone();
    }
    #endregion
  }
commented: Thanks! +1

Thanks sknake, I was trying to clone the first class that I posted. I was trying to clone each individual method 'Name, CurrentLocationID, etc..' Here's my updated code that you recommended:

public class Employeex : ICloneable
    {
        // using properties instead of the constructor
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public int CurrentLocationID { get; set; }
        public Employeex()
        {
        }
        public object Clone()
        {
            return this.MemberwiseClone();
        }
        public Employeex Clone1()
        {
            return (Employeex)((ICloneable)this).Clone();
        }

    }

I made a couple of changes, because the class can't be sealed. I've made an array containing all of the information under employeeid, name, etc, and used a foreach statement to output it onto the screen. How would I output it using the clone method?

Thanks for the help.

Why did you rename Clone to Clone1 and remove the explicit interface implementation? That doesn't make any sense. Control clone through the interface like I posted and call the clone method. ICloneable returns an objects but if you do it like I posted you can have a strongly typed clone while implementing ICloneable...

public class Employeex : ICloneable
  {
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public int CurrentLocationID { get; set; }
    public Employeex()
    {
    }
    #region ICloneable Members
    object ICloneable.Clone()
    {
      Employeex clone = new Employeex();
      clone.EmployeeID = this.EmployeeID;
      clone.Name = this.Name;
      clone.CurrentLocationID = this.CurrentLocationID;
      return clone;
    }
    public Employeex Clone()
    {
      return (Employeex)((ICloneable)this).Clone();
    }
    #endregion
  }

Calling it:

private void button2_Click(object sender, EventArgs e)
    {
      Employeex emp1 = new Employeex();
      emp1.EmployeeID = 50;
      emp1.Name = "emp1";
      Employeex emp2 = emp1.Clone();
      emp2.Name = "emp2";
      System.Diagnostics.Debugger.Break();
    }

Thanks for the help, and sorry about the confusion, the ICloneable interface is pretty confusing to me. I tried to call it, but it's giving the error 'cannot convert type object to Employeex[]. an explicit conversion exits (are you missing a cast?)' Here is my code:

public static void AnotherMethod()
        {
            Console.WriteLine(Environment.NewLine + Environment.NewLine);

            Employeex[] empArray1 = new Employeex[6];
            empArray1[0].EmployeeID = 123; empArray1[0].Name = "Fred Flinstone"; empArray1[0].CurrentLocationID = 1;
            Employeex[] empArray2 = empArray1.Clone();
            empArray1[1].EmployeeID = 124; empArray1[1].Name = "Barney Rubble"; empArray1[1].CurrentLocationID = 1;

            foreach (Employeex e in EmployeeArray)
                Console.WriteLine("Next employee pulled: " + e.Name);


        }

I don't see what clone has to do with this, but:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace daniweb
{
  public class Employeex : ICloneable
  {
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public int CurrentLocationID { get; set; }
    public Employeex()
    {
    }
    public Employeex(string Name, int EmployeeId, int CurrentLocationID)
      : base()
    {
      this.Name = Name;
      this.EmployeeID = EmployeeID;
      this.CurrentLocationID = CurrentLocationID;
    }
    #region ICloneable Members
    object ICloneable.Clone()
    {
      Employeex clone = new Employeex();
      clone.EmployeeID = this.EmployeeID;
      clone.Name = this.Name;
      clone.CurrentLocationID = this.CurrentLocationID;
      return clone;
    }
    public Employeex Clone()
    {
      return (Employeex)((ICloneable)this).Clone();
    }
    #endregion
    public string GetEmployeeInformation()
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendLine(string.Format("Employee: {0}", this.Name));
      sb.AppendLine(string.Format("ID: {0:F0}", this.EmployeeID));
      sb.Append(string.Format("Location: {0:F0}", this.CurrentLocationID));
      return sb.ToString();
    }
  }
}

Calling it:

private void button2_Click(object sender, EventArgs e)
    {
      List<Employeex> lst = new List<Employeex>();
      lst.Add(new Employeex("Scott", 1, 5));
      lst.Add(new Employeex("Dan", 2, 5));
      lst.Add(new Employeex("Ken", 3, 5));
      lst.Add(new Employeex("Kevin", 4, 6));
      foreach (Employeex emp in lst)
      {
        Console.WriteLine(emp.GetEmployeeInformation());
      }
    }

Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.