473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Can I write Copy Constructor in VB?

Hi, guys.
I'm debugging a VB program that is written by the guy who already left the company. I found out the main problem is that the guy used object assignment which will mess up the data. For example, I have a customized class as following:

Expand|Select|Wrap|Line Numbers
  1. Public Class testCell
  2.     Private _name As String = ""
  3.     Private isCopied As Boolean = False
  4.  
  5.     Public Property Name() As String
  6.         Get
  7.             Return _name
  8.         End Get
  9.         Set(ByVal Value As String)
  10.             _name = Value
  11.         End Set
  12.     End Property
  13.  
  14.     Public Property isCopy() As Boolean
  15.         Get
  16.             Return isCopied
  17.         End Get
  18.         Set(ByVal Value As Boolean)
  19.             isCopied = Value
  20.         End Set
  21.     End Property
  22.  
  23.     Public Sub Reset()
  24.         _name = ""
  25.         isCopied = False
  26.     End Sub
  27. End Class
  28.  
And in my main program, I have:

Expand|Select|Wrap|Line Numbers
  1.         Dim cell1 As testCell = New testCell
  2.         Dim cell2 As testCell = New testCell
  3.  
  4.         cell1 = cell2
  5.  
  6.         cell2.Name = "Ben"
  7.         If cell1.Name = "Ben" Then
  8.             MsgBox("Object is referenced")
  9.         End If
  10.  
Run the main program, I will get the message box pop up. But this is not what I want. All I want is to copy property values in cell2 to cell1, instead of reference cell2 to cell1.

Now I know I can use:

Expand|Select|Wrap|Line Numbers
  1. cell1.Name = cell2.Name
  2. cell1.isCopy = cell2.isCopy
  3.  
to achieve what i really want. But in real life, if the class has hundreds of properties, this is not the right way to do it. So I want to create a copy constructor as I can do in C++, anybody knows how to do it in VB?




Ben
Jan 11 '07 #1
5 3645
willakawill
1,646 1GB
Hi Ben
In your assignment,
cell1 = cell2
you would not use a copy constructor. You would overload the '=' operator.
Jan 11 '07 #2
Hi Ben
In your assignment,
cell1 = cell2
you would not use a copy constructor. You would overload the '=' operator.

Yep. Actually, I just figure out how i could write the copy constructor.

Expand|Select|Wrap|Line Numbers
  1. Public Class testCell
  2.     Private _name As String
  3.     Private isCopied As Boolean
  4.  
  5.     Public Property Name() As String
  6.         Get
  7.             Return _name
  8.         End Get
  9.         Set(ByVal Value As String)
  10.             _name = Value
  11.         End Set
  12.     End Property
  13.  
  14.     Public Property isCopy() As Boolean
  15.         Get
  16.             Return isCopied
  17.         End Get
  18.         Set(ByVal Value As Boolean)
  19.             isCopied = Value
  20.         End Set
  21.     End Property
  22.  
  23.     Public Sub Reset()
  24.         _name = ""
  25.         isCopied = False
  26.     End Sub
  27.  
  28.     Public Sub New()
  29.         _name = ""
  30.         isCopied = False
  31.     End Sub
  32.  
  33.     Public Sub New(ByVal cell As testCell)
  34.         _name = cell.Name
  35.         isCopied = cell.isCopy
  36.     End Sub
  37. End Class
  38.  

Now, in my main program

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Dim cell1 As testCell = New testCell
  3.         Dim cell2 As testCell = New testCell
  4.  
  5.         cell2.Name = "Ben"
  6.         cell1 = New testCell(cell2)
  7.         cell2.Name = "William"
  8.  
  9.         If cell1.Name = "William" Then
  10.             MsgBox("Object is referenced")
  11.         End If
  12.  
  13.  
Running this main program, the message box will not pop up. And that's exactly what i want.
Jan 11 '07 #3
willakawill
1,646 1GB
Yep. Actually, I just figure out how i could write the copy constructor.

Expand|Select|Wrap|Line Numbers
  1. Public Class testCell
  2.     Private _name As String
  3.     Private isCopied As Boolean
  4.  
  5.     Public Property Name() As String
  6.         Get
  7.             Return _name
  8.         End Get
  9.         Set(ByVal Value As String)
  10.             _name = Value
  11.         End Set
  12.     End Property
  13.  
  14.     Public Property isCopy() As Boolean
  15.         Get
  16.             Return isCopied
  17.         End Get
  18.         Set(ByVal Value As Boolean)
  19.             isCopied = Value
  20.         End Set
  21.     End Property
  22.  
  23.     Public Sub Reset()
  24.         _name = ""
  25.         isCopied = False
  26.     End Sub
  27.  
  28.     Public Sub New()
  29.         _name = ""
  30.         isCopied = False
  31.     End Sub
  32.  
  33.     Public Sub New(ByVal cell As testCell)
  34.         _name = cell.Name
  35.         isCopied = cell.isCopy
  36.     End Sub
  37. End Class
  38.  

Now, in my main program

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Dim cell1 As testCell = New testCell
  3.         Dim cell2 As testCell = New testCell
  4.  
  5.         cell2.Name = "Ben"
  6.         cell1 = New testCell(cell2)
  7.         cell2.Name = "William"
  8.  
  9.         If cell1.Name = "William" Then
  10.             MsgBox("Object is referenced")
  11.         End If
  12.  
  13.  
Running this main program, the message box will not pop up. And that's exactly what i want.
You are using a default copy constructor which can lead to unpredictable results
Jan 11 '07 #4
You are using a default copy constructor which can lead to unpredictable results

But other than that, what could i do?
Jan 11 '07 #5
willakawill
1,646 1GB
But other than that, what could i do?
You write a sub and pass the two instances by ref
Jan 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.