Visual Basic/Object Oriented Programming
|
|
|
|
|
Page 2 of 5 You can think of a type as a simple form of class. Types can only hold variables, not procedures/functions. An important distinction between user defined types ('UDT') and and 'classes' in 'VB6' is that 'UDTs' are value types while 'classes' are reference types. This means that when you store an object ('instance' of a 'class') in a variable what actually happens is that a pointer to the existing object is stored but when you store a 'UDT' instance in a variable a copy of the instance is stored. For instance imagine that you have defind a class, 'Class1', and a UDT, 'Type1':
Dim ac As Class1 Dim bc As Class1 Dim at As Type1 Dim bt As Type1
Now assign ac to bc:
Set bc = ac
and at to bt
bt = at
Now make a change to one of the properties of bc and look at the corresponding property in ac. You should see that when you change bc that ac also changes. This is because ac and bc are really pointers to the same block of memory. If you do the same exercise with bt and at you should see that changing properties of bt does not affect at. This is because at and bt are values not references and occupy distinct locations in memory.
This is important when storing objects in 'Collections'. If you add an object to a collection you can later change the values of its properties and the object in the collection will also seem to change. in fact it is the same object because what is stored in the collection is the reference ('pointer') to the object. However if you store a 'UDT' in a collection what is stored is a copy of the values not a reference so changing the original UDT will not affect the content of the Collection.
|