Can I write Copy Constructor in VB?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benfly08
    New Member
    • Dec 2006
    • 19

    #1

    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:

    Code:
    Public Class testCell
        Private _name As String = ""
        Private isCopied As Boolean = False
    
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal Value As String)
                _name = Value
            End Set
        End Property
    
        Public Property isCopy() As Boolean
            Get
                Return isCopied
            End Get
            Set(ByVal Value As Boolean)
                isCopied = Value
            End Set
        End Property
    
        Public Sub Reset()
            _name = ""
            isCopied = False
        End Sub
    End Class
    And in my main program, I have:

    Code:
            Dim cell1 As testCell = New testCell
            Dim cell2 As testCell = New testCell
    
            cell1 = cell2
    
            cell2.Name = "Ben"
            If cell1.Name = "Ben" Then
                MsgBox("Object is referenced")
            End If
    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:

    Code:
    cell1.Name = cell2.Name
    cell1.isCopy = cell2.isCopy
    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
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi Ben
    In your assignment,
    cell1 = cell2
    you would not use a copy constructor. You would overload the '=' operator.

    Comment

    • benfly08
      New Member
      • Dec 2006
      • 19

      #3
      Originally posted by willakawill
      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.

      Code:
      Public Class testCell
          Private _name As String
          Private isCopied As Boolean
      
          Public Property Name() As String
              Get
                  Return _name
              End Get
              Set(ByVal Value As String)
                  _name = Value
              End Set
          End Property
      
          Public Property isCopy() As Boolean
              Get
                  Return isCopied
              End Get
              Set(ByVal Value As Boolean)
                  isCopied = Value
              End Set
          End Property
      
          Public Sub Reset()
              _name = ""
              isCopied = False
          End Sub
      
          Public Sub New()
              _name = ""
              isCopied = False
          End Sub
      
          Public Sub New(ByVal cell As testCell)
              _name = cell.Name
              isCopied = cell.isCopy
          End Sub
      End Class

      Now, in my main program

      Code:
              Dim cell1 As testCell = New testCell
              Dim cell2 As testCell = New testCell
      
              cell2.Name = "Ben"
              cell1 = New testCell(cell2)
              cell2.Name = "William"
      
              If cell1.Name = "William" Then
                  MsgBox("Object is referenced")
              End If
      Running this main program, the message box will not pop up. And that's exactly what i want.

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by benfly08
        Yep. Actually, I just figure out how i could write the copy constructor.

        Code:
        Public Class testCell
            Private _name As String
            Private isCopied As Boolean
        
            Public Property Name() As String
                Get
                    Return _name
                End Get
                Set(ByVal Value As String)
                    _name = Value
                End Set
            End Property
        
            Public Property isCopy() As Boolean
                Get
                    Return isCopied
                End Get
                Set(ByVal Value As Boolean)
                    isCopied = Value
                End Set
            End Property
        
            Public Sub Reset()
                _name = ""
                isCopied = False
            End Sub
        
            Public Sub New()
                _name = ""
                isCopied = False
            End Sub
        
            Public Sub New(ByVal cell As testCell)
                _name = cell.Name
                isCopied = cell.isCopy
            End Sub
        End Class

        Now, in my main program

        Code:
                Dim cell1 As testCell = New testCell
                Dim cell2 As testCell = New testCell
        
                cell2.Name = "Ben"
                cell1 = New testCell(cell2)
                cell2.Name = "William"
        
                If cell1.Name = "William" Then
                    MsgBox("Object is referenced")
                End If
        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

        Comment

        • benfly08
          New Member
          • Dec 2006
          • 19

          #5
          Originally posted by willakawill
          You are using a default copy constructor which can lead to unpredictable results

          But other than that, what could i do?

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by benfly08
            But other than that, what could i do?
            You write a sub and pass the two instances by ref

            Comment

            Working...