Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

VB6 question. Is there an easy way to copy member variables

Status
Not open for further replies.

kender

Advanced Member level 4
Joined
Jun 19, 2005
Messages
1,425
Helped
138
Reputation
276
Reaction score
39
Trophy points
1,328
Location
Stanford, SF Bay Peninsula, California, Earth, Sol
Activity points
10,035
VB6 question. Is there an easy way to copy member variables during object assignment instead of making a reference?



Colleagues,

I wasn’t able to find a definitive answer to this one on the web. Is there a way in VB6 to make a copy of an object without writing a copy function? What’s the best way to go about it?

Thanks,
- Nick

P.S. I’m writing my first VB6 classes. In C++ the default assignment operator copies the member variables to the new object. May be that’s what’s confusing me.
 

referring to a member variable should be the easieast way to access class' private variables through public interface. Just provide Public property let and get functions to access class private variables.


Example:

'Class cCircle

'Class attributes
Private mX as single
Private mY as single
Private mRadius as single


'Then make a public interface to access these private variables

Public property Let centerX(x as single)
mX = x
end property

'etc
Public property Get centerX() as single
centerX = mX
end property

'etc.


'So can easily access These variables like
dim myCircle as new cCircle

myCircle.centerX =2.1
myCircle.centerY =2.5

'Its quite simple and straight forward
 

Re: VB6 question. Is there an easy way to copy member varia

Thanks, but that's not what I was asking about. I want to make a copy of an object without having to assign every member variable manually. I.e. I don't want to do the following:

Code:
objCopyTo.m_a = objCopyFrom.m_a
objCopyTo.m_b = objCopyFrom.m_b
'etc
 

You may copy the entire object.

'Example
Set myObject = previousObject

In this case you copy all attributes of teh object. This is slow however particularly if your object has a large set of codes.

And be sure to release the objects memory in the scope you are working. VB6 has no automatic garbage collection for objects.

Simply set the object to nothing after using:

Set myObject = Nothing

'then exit subroutine
 

Re: VB6 question. Is there an easy way to copy member varia

neoaspilet11 said:
You may copy the entire object.

'Example
Code:
Set myObject = previousObject

In this case you copy all attributes of teh object. This is slow however particularly if your object has a large set of codes.

You clearly haven't tried what you are proposing! :mad:
The line of code you've posted assigns a reference to previousObject. It doesn't make a copy of member variables. If after that line you change an attribute of previousObject that attribute of myObject will change also. E.g.:

Code:
Set myObject = previousObject
myObject.m_x = 1
previousObject.m_x = 2
Debug.Print myObject.m_x  'this will print 2
 

You first declare myobject as new instance of teh class;

Dim myObject as New cClass

'then you can use
Set myObject = previousObject

If you simply declare it without using new then that would happen like what you have done.

Hope this helps a bit.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top