Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Simple Cloning Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan Dorey - 21 Sep 2007 16:33 GMT
I've implemented the ICloneable interface on a class of mine. I've
implemented it in two different ways both of which I feel should work
correctly, but only one does and I'd be curious to know why.

Working:

           CallflowBase ret = null;
           ret = this.MemberwiseClone() as CallflowBase;
           ret.m_callflowName = this.m_callflowName;

/ * I'm resetting these values since I want to make sure the user
reintializes the cloned object before using it */
           ret.m_initialized = false;
           ret.m_deviceID = null;
           ret.m_currentNode = null;
           ret.m_asyncCommandProcessor = null;
           ret.m_syncCommandProcessor = null;

           if (this.m_rootNode != null)
           {
               ret.m_rootNode = this.m_rootNode.Clone() as INode;
           }

NOT Working:

/* I figured it would be easier to just create a new instance since I
don't want to copy most of the members */

           CallflowBase ret = new CallflowBase();
           ret.m_callflowName = this.m_callflowName;

           if (this.m_rootNode != null)
           {
               ret.m_rootNode = this.m_rootNode.Clone() as INode;
           }

But it turns out that I'm not actually making a proper copy because I
end up pointing to the same object! Am I missing something obvious
here?

Thanks,
Dan
Chris Shepherd - 21 Sep 2007 16:58 GMT
> I've implemented the ICloneable interface on a class of mine. I've
> implemented it in two different ways both of which I feel should work
[quoted text clipped - 35 lines]
> end up pointing to the same object! Am I missing something obvious
> here?

Yes, in one call *ret* is assigned to a MermerwiseClone of *this*, in
the other it's just assigned to *this*.

Chris.
Dan Dorey - 21 Sep 2007 18:15 GMT
Chris,

I don't follow you.

In the example code that works I'm doing:

ret = this.MemberwiseClone() as CallflowBase;

In the example that doesn't work I'm doing:

CallflowBase ret = new CallflowBase();

So I'm not just assigning it to this....

Dan
Chris Shepherd - 21 Sep 2007 19:28 GMT
> Chris,
>
[quoted text clipped - 11 lines]
>
> Dan

Apologies, I misread the code the first time.

You are only posting a partial code example, and your statements about
what will work and what won't are not in line with anything else I've
seen when working with ICloneable.

Below is a short but concise example that demonstrates both methods work
just fine.

This begs the question though, how are you determining whether or not
you're actually using the same reference?

Chris.

class ClassA : ICloneable
{
    public string name;

    public ClassA()
    {
        name = "ClassA";
    }

    public object Clone()
    {
        ClassA ret = null;
        ret = (ClassA)this.MemberwiseClone();
        return ret;
    }
}

class ClassB : ICloneable
{
    public string name;

    public ClassB()
    {
        name = "ClassB";
    }

    public object Clone()
    {
        ClassB ret = new ClassB();
        ret.name = this.name;
        return ret;
    }
}

class General
{
    [STAThread]
    static void Main(string[] args)
    {
        ClassA a1 = new ClassA();
        a1.name = "The first ClassA";

        ClassA a2 = (ClassA)a1.Clone();
        a2.name = "The second ClassA";

        MessageBox.Show(a1.name + "\n" + a2.name);

        ClassB b1 = new ClassB();
        b1.name = "The first ClassB";

        ClassB b2 = (ClassB)b1.Clone();
        b2.name = "The second ClassB";

        MessageBox.Show(b1.name + "\n" + b2.name);
    }
}
Peter Duniho - 21 Sep 2007 19:42 GMT
> I've implemented the ICloneable interface on a class of mine. I've
> implemented it in two different ways both of which I feel should work
> correctly, but only one does and I'd be curious to know why.

Please don't post the same message twice.  I've already posted a reply
to the other thread you created with the same exact question.  Now we
have what is essentially the same thread split into two.  It will be
very difficult to correlate various replies from different people.
Dan Dorey - 21 Sep 2007 20:17 GMT
> > I've implemented the ICloneable interface on a class of mine. I've
> > implemented it in two different ways both of which I feel should work
[quoted text clipped - 4 lines]
> have what is essentially the same thread split into two.  It will be
> very difficult to correlate various replies from different people.

Peter. Sorry for posting the same message twice. I posted the original
but after waiting 30 minutes I was still unable to view the post.
Google said that it had been removed or something strange like that. I
assumed that no one else would have been able to read it either.
Apparently that has been resolved now.

As long as the code I posted makes sense, then I'm sure I've done
something stupid outside of the posted code. I'll investigate this
further. Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.