Results 1 to 2 of 2

Thread: Copy constructor in VB.NET

Hybrid View

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Copy constructor in VB.NET

    In C++ it is possible to write copy constructors such that if you do this:

    TypeT a;
    TypeT b;

    a=b;

    a is a created copy of b. This is simple as long as all the members of TypeT are simple types, don't allocate memory, etc.

    How does copy construction work in .NET? As long as all the members of a class are simple, there should be no issues. Perhaps there are never any issues, because things like arrays, that might be handled by pointers in C++ could be sized and copied transparently during construction in .NET, but it seems to me that you could still create a class where it would be nearly impossible to simply copy everything.
    My usual boring signature: Nothing

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Some suggestions (treat them all as separate strategies):

    1. Inherit the ICloneable interface and use a=b.clone()

    2. consider making your class a structure (with methods) instead (if your program allows this, it might not)

    3. Use binary serialisation to make an exact duplicate of your class.

    I can give you some sample code for number 3 if you want it. Its just a single function that you drop into your class. This is probably the easiest way to get an *identical* copy without any differences at all. Although it might come with some overhead if your class is complex.

    PM me if you want the code.
    I don't live here any more.

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