Results 1 to 10 of 10

Thread: Copy an object reference..?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    100

    Copy an object reference..?

    In theory..I'd like to do the following..copy object a's properties over to object b, then be able to modify b's properties without affecting object a. I'm just using a Panel for the example, basically I'd like to do this with any class..

    Code:
    	Panel a = new Panel(x);
    	a.Tag = 0;
    
    	//Real code
    	Panel a = a;
    
    	//Theory code (wrong syntax but is there a way to do this?)
    	Panel b = new b;
    
    	b.Tag = 1;
    
    	int ia = (int) a.Tag;
    	int ib = (int) b.Tag;
    
    	//real output ia = 0
    	//real output ib = 0
    
    	//theory output ia = 0
    	//theory output ib = 1

    Specifically I have a TabControl and I want to define a Tab Page and whats on it, but I'd like to be able to copy the tab page and create a new one so it has the same design/controls on it.

    I'm sure I can't have the same exact control on the form at the same time so I could do Form.Controls.Remove for one then add the other? Or maybe I'm going the wrong way on this...


    Thanks,
    Mitchel

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Try Clone.
    Or is it called something else in C#? There is some function like that, it's called clone in Java, so I guess it would be Clone in C#.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Try Clone.
    Or is it called something else in C#? There is some function like that, it's called clone in Java, so I guess it would be Clone in C#.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Try Clone.
    Or is it called something else in C#? There is some function like that, it's called clone in Java, so I guess it would be Clone in C#.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I haven't checked, but I thought it was Copy() in .Net instead of clone....not sure though. Will check later.

  6. #6
    Member
    Join Date
    Sep 2002
    Location
    London
    Posts
    63
    I have used it and it is object.Clone().

    i.e. then you would be creating a copy of the object rather than a reference to it.

    Some special types i.e. SortedList may not have a .Clone method in which case you have to create your own by extending the class.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    100
    Hey thanks guys, the clone doesn't work though or at least it's not on the object i'm trying to clone, I can try to figure out how to clone it..but I'm no expert, maybe theres another way?

    Thanks,
    Mitchel

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    100
    I implemented .Clone() using return this.MemberWiseClone(); however it doesn't work properly...

    Perhaps I'm going about this the wrong way..

    Basically I have a list of mail boxes and i want to double click one and open up a new tab page to administrate it.. and I want to be able to open as many mail boxes as I want at the same time and modify each.

    Perhaps I should just keep it to one mail box at a time? I'd really rather not, I think this is a good flow for the application.

    Thanks,
    Mitchel

  9. #9
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    How about this: (this is kind of the way the windows form designer works)

    Code:
    public class myPanel : Panel
    {
        public myPanel()
        { 
             // set our default properties here.
             // if the constructor takes an argument,
             // you'll need to change this.
             Tag = "foo";
             Caption = "123";
        }
    }
    Code:
    Panel a = (Panel)(new MyPanel());
    Panel b = (Panel)(new MyPanel());
    this is assuming you don't want to change the default properties at run time.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    100
    I like the idea, it might be a bit odd to use though. The panel..hmm if I did that It would be hard to edit the text boxes and fields on the panel, but I see where I can go with this.

    I'll definately look into it. Thanks for the idea!

    Mitchel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width