Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set myObject = oldObject?

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I am having a problem with objects. I'll illustrate with a simple problem

'========================================
clsSimple 'this is just a simple class
private prop1 as long

public property let setProp1(m as long)
prop1 = m
end property
public property get getProp1() as long
getProp11 = prop1
end property
'======================

In the program I have:

dim a as clsSimple
dim b as clsSimple

a.setprop1 = 45

set b = a

This is where I am stuck. What the line set b = a does is create a reference to object a. I want to create another instance of it. Basically I want b to have all the values that a has but not to be related to it in any way, How do I do this? Do I have to:

set b = new clsSimple
b.setprop1 = a.getprop1

I have tried:
set b = new clsSimple
set b = a

But that did not work....

Any ideas.... Troy Williams B.Eng.
fenris@hotmail.com

 
I know about the implements keyword and it is not what I am looking for. Implements is VB's attempt at polymorphism (from my java classes though, it looks like inheritance).

This is what I want to accomplish:

I have an object ,A, that already has all it's class variables filled with values (from my inital post prop1 would have a value of say 67876). What I want to be able to do is create another, empty object (i.e. prop1 would be set to the default of 0) and make it equal to A, but not a reference. I want a completely new object in memory but with the same information that A has.

I can do it like this:
dim b as clsSimple
set b = new clsSimple
b.setProp1 = a.getProp1


The problem with the above code is; what happens if there are thousands of properties to set? Troy Williams B.Eng.
fenris@hotmail.com

 
You can't create new instances of an object in the manner you want, unfortunately.

Set a = b

will always create a pointer to the same object.

Also, you seem to misunderstand properties:

Private clsprop1 as Long

Public Property Get prop1 as Long
prop1 = clsprop1
End Property

Public Property Let prop1(lprop1 as Long)
clsprop1 = lprop1
End Property

is the correct way to use properties, allowing you to say

obj.Prop1 = value

instead of obj.SetProp value

Chaz
 
Thanks for the pointers, it's too bad that VB can't do that with objects.

I didn't realize that is how properties where meant to be used. The reason that I used properties the way I did was based on my java experience.

From what I can see, I don't see an advantage to the "correct" way of using properties. I differentiate the get and let properties by prefixing get or set to the name of the property to identify what the property is supposed to do (either allowing a class variable to be set or retrieving the information contained in a class variable). I believe the method that you advocate is the polymorphic VB in action.

I would be interested in hearing if there is a performance difference between the different methods of properties, or are both ways exceptable?

Troy Williams B.Eng.
fenris@hotmail.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top