?
Solved

Set keyword

Posted on 2005-04-15
1
Medium Priority
?
219 Views
Last Modified: 2010-05-02

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
Comment
Question by:TungVan
1 Comment
 
LVL 38

Accepted Solution

by:
PaulHews earned 1000 total points
ID: 13793136
>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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question