• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Set keyword


Hi,

I have a CButton class.

I created an instance of it called                         Dim mButton as CButton

I have a Temporary button object called             Dim mTemp as CButton

I also have a AddButton function that takes 2 arguments: 1 flag, and 1 Button object


------------------------------------------------------------------------------------------------------------------------
AddButton False, mTemp

Public Sub AddButton(ByVal NewButtonFlag As Boolean, Optional ByVal lButton As Object)

    Dim lButtonIndex As Integer
    lButtonIndex = myMapForm!cmdButton.UBound + 1
    ReDim Preserve mButtonArray(lButtonIndex)
   
    'Flag = true means that we create a new button and put it into the array
    If NewButtonFlag = True Then
        Dim myButton As CButton
        Set myButton = New CButton
        Set mButtonArray(lButtonIndex) = myButton
        Set myButton = Nothing
       
    'Flag = false means that we copied the button in the function parameter into the array
    ElseIf NewButtonFlag = False Then
        Set mButtonArray(lButtonIndex) = lButton
   
    Else
        'do nothing
       
    End If
   
    Load myMapForm!cmdButton(lButtonIndex)
    myMapForm!cmdButton(lButtonIndex).Visible = True
    myMapForm!cmdButton(lButtonIndex).Left = 0
    myMapForm!cmdButton(lButtonIndex).Top = 0
    myMapForm!cmdButton(lButtonIndex).ZOrder
    PaintButton (lButtonIndex)



End Sub
------------------------------------------------------------------------------------------------------------------------

But Set keyword set the pointer of mButtonArray(lButtonIndex) to lButton, and in this case, is mTemp.

Everytime I modify mTemp properties, it also set the properties of mButtonArray(lButtonIndex)....since it copies the reference or pointer...

But that's not I wish to have. I just want to copy properties of mTemp to mButtonArray(lButtonIndex)...but I want them to be independent...If I set the properties of mTemp, I don't want it to automatically set the properties of mButtonArray(lButtonIndex)

Do you have a idea for that? Except copying manually...like this
------------------------------------------------------------------------------------------------------------------------
    Dim lButtonIndex As Integer
    lButtonIndex = myMapForm!cmdButton.UBound + 1
    ReDim Preserve mButtonArray(lButtonIndex)
    Dim myButton As CButton
   
    'Flag = true means that we create a new button and put it into the array
    If NewButtonFlag = True Then
        Set myButton = New CButton
        Set mButtonArray(lButtonIndex) = myButton
        Set myButton = Nothing
       
    'Flag = false means that we copied the button in the function parameter into the array
    ElseIf NewButtonFlag = False Then
        Set myButton = New CButton
        myButton.ButtonType = lButton.ButtonType
        myButton.CControl_ControlLabel = lButton.CControl_ControlLabel
        Set mButtonArray(lButtonIndex) = myButton
        Set myButton = Nothing
------------------------------------------------------------------------------------------------------------------------
This works ok...But i have to copy each attribute manually one at a time....It's a bit tedious....Is there a better way to do it?


Thanks

0
TungVan
Asked:
TungVan
1 Solution
 
PaulHewsCommented:
>This works ok...But i have to copy each attribute manually one at a time....It's a bit tedious....Is there a better way to do it?

When I need this functionality, I provide a Clone method to my object:

Public Function Clone() as CButton
  Dim B as new CButton
  B.ButtonType = Me.ButtonType
  B.CControl_ControlLabel = Me.CControl_ControlLabel
... etc.



  Set Clone = B
End Function

The thing is that you do have to do these things by hand and that's always a problem with object classes in any language.  Your object could contain complex data, including other objects, collections, etc, so the compiler doesn't know how to automatically create copies.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now