Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copying an instance of a class

9 views
Skip to first unread message

S Figueroa

unread,
Apr 19, 2002, 5:12:46 PM4/19/02
to
I had created a class to store information collected through a form.
However, I will need two instances of this class, one that will store the
final information and other to keep the temporary changes. What I want to
do is to assign (copying) the properties of the final instance to the
temporary instance without having to write each property, using an
instruction such as:

Set TempInstance = FinalInstance

Also, I want to be able to change the temporary instance without affecting
the final instance with the changes. The final instance should be modified
only when the end-user presses the save button to confirm the change.

Thank you


Jeff Johnson

unread,
Apr 19, 2002, 5:11:31 PM4/19/02
to

"S Figueroa" <sfig...@informedika.com> wrote in message
news:eXUrwW#5BHA.2156@tkmsftngp05...

> What I want to
> do is to assign (copying) the properties of the final instance to the
> temporary instance without having to write each property,

This has been asked several times before. The answer: sorry, not gonna
happen. You MUST copy the properties individually.

Search the newsgroup for keywords like "copy class object" for some broader
explanations and examples.


Larry Serflaten

unread,
Apr 19, 2002, 7:08:46 PM4/19/02
to
"S Figueroa" <sfig...@informedika.com> wrote in message
> What I want to
> do is to assign (copying) the properties of the final instance to the
> temporary instance without having to write each property, using an
> instruction such as:
>
> Also, I want to be able to change the temporary instance without affecting
> the final instance with the changes. The final instance should be modified
> only when the end-user presses the save button to confirm the change.
>
> Thank you
>
>

You can see if this will fit your needs. For the demo, add a button to Form1
and add a Class to the project using default names.

' [ Form1 code ]

Option Explicit
Private objA As Class1

Private Sub Form_Load()
' Build object with data
Set objA = New Class1
objA.Name = "Data Object"
objA.Age = 10
objA.Data(7) = 1
End Sub

Private Sub Command1_Click()
Dim objB As New Class1
Cls

' Copy object to local variable
objB.Copy = objA.Copy

' Change local copy
objB.Name = "New Object"
objB.Age = 20
objB.Data(7) = 255
Print "COPIES:"
Display objA, objB

' Save changes
objA.Copy = objB.Copy
Print "CHANGED:"
Display objA, objB

End Sub

Private Sub Display(A As Class1, B As Class1)
' Display both
Print A.Name
Print A.Age
Print A.Data(7)
Print
Print B.Name
Print B.Age
Print B.Data(7)
Print: Print
End Sub

' [ Class1 code ]

Option Explicit

Private Type Settings
Name As String * 20
Age As Long
City As String * 20
State As String * 20
Bytes(0 To 7) As Byte
End Type

Private Type CopyData
Data As String * 68
End Type

Private My As Settings

Public Property Get Name() As String
Name = My.Name
End Property

Public Property Let Name(ByVal Value As String)
My.Name = Value
End Property

Public Property Get Age() As Long
Age = My.Age
End Property

Public Property Let Age(ByVal Value As Long)
My.Age = Value
End Property

Public Property Get Data(ByVal Index As Long) As Byte
Data = My.Bytes(Index)
End Property

Public Property Let Data(ByVal Index As Long, Value As Byte)
My.Bytes(Index) = Value
End Property

' Add Public properties as needed

Public Property Get Copy() As String
Dim Copier As CopyData
LSet Copier = My
Copy = Copier.Data
End Property

Public Property Let Copy(ByRef Data As String)
Dim Copier As CopyData
Copier.Data = Data
LSet My = Copier
End Property


For primative types it works OK....

LFS

What is the KB?: http://support.microsoft.com/default.aspx?ln=EN-US&pr=kbinfo&
RTFM? http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbcon98/vbstartpage.htm
Where's the archive?: http://groups.google.com/advanced_group_search?q=group:microsoft.public.vb.*&hl=en&safe=off&num=50


Bob O`Bob

unread,
Apr 19, 2002, 7:23:54 PM4/19/02
to
S Figueroa wrote:
>
> What I want to
> do is to assign (copying) the properties of the final instance to the
> temporary instance without having to write each property, using an
> instruction such as:
>
> Set TempInstance = FinalInstance


There actually is (sort of) a way to do this.
But you'd still have to rewrite the class.

If all of the class' local storage is kept in elements
of an instance of a UDT, then you can expose the UDT
through property procedures.


Bob
--
posting from work, but representing only myself

0 new messages