class - Assignment of objects in VB6 -


i attempting create 2 identical objects in vb6 assignment statements; this...

dim myobj1 class1 dim myobj2 class1  set myobj1 = new class1 myobj1.myval = 1 set myobj2 = myobj1 

it has become apparent doesn't create 2 objects rather 2 references same object, isn't after. there way create second object in sort of way, or have copy object 1 member @ time...

set myobj2 = new class1 myobj2.mem1 = myobj1.mem1 ... 

?

edit 2 scott whitlock has updated excellent answer , have incorporated changes now-working code snippet.

private type mymemento      value1 integer      value2 string end type  private memento mymemento  public property let myval(byval newval integer) memento.value1 = newval end property  public property myval() integer myval = memento.value1 end property  friend property let setmemento(new_memento mymemento)     memento = new_memento end property  public function copy() class1      dim result class1      set result = new class1      result.setmemento = memento      set copy = result end function 

one performs assignment in code thus...

set mysecondobj = myfirstobj.copy 

like many modern languages, vb6 has value types , reference types. classes define reference types. on other hand, basic types integer value types.

the basic difference in assignment:

dim integer dim b integer = 2 b = a = 1 

the result a 1 , b 2. that's because assignment in value types makes copy. that's because each variable has space allocated value on stack (in case of vb6, integer takes 2 bytes on stack).

for classes, works differently:

dim myclass dim b myclass set = new myclass a.value1 = 2 set b = a.value1 = 1 

the result both a.value1 , b.value1 1. that's because state of object stored in heap, not on stack. reference object stored on stack, set b = a overwrites reference. interestingly, vb6 explicit forcing use set keyword. other modern languages don't require this.

now, can create own value types (in vb6 they're called user defined types, in other languages they're called structs or structures). here's tutorial.

the differences between class , user defined type (aside class being reference type , udt being value type) class can contain behaviors (methods , properties) udt cannot. if you're looking record-type class, udt may solution.

you can use mix of these techniques. let's need class because have behaviors , calculations want include along data. can use memento pattern hold state of object inside of udt:

type mymemento     value1 integer     value2 string end type 

in class, make sure all internal state stored inside private member of type mymemento. write properties , methods use data in 1 private member variable.

now making copy of object simple. write new method on class called copy() returns new instance of class , initialize copy of own memento:

private memento mymemento  friend sub setmemento(newmemento mymemento)     memento = newmemento end sub  public function copy() myclass     dim result myclass     set result = new myclass     call result.setmemento(memento)     set copy = result end function 

the friend hides stuff outside project, doesn't hide setmemento sub, it's can vb6.

hth


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -