On Mar 19, 2007, at 19:25 UTC, Giovanni wrote:

> Can you expand on it Joe...

OK.  Let's start with something easier, say, integers.  When you
declare an integer property or local variable:

  Dim i As Integer

this sets aside a little storage location big enough to hold an
integer.  By default, the value stored there is nil, but you can easily
store something else by using the assignment operator:

  i = 42

at which point, the value stored in i is now 42.  I know this is
elementary, but it sets us up nicely to consider an object reference:

  Dim d As Dictionary

This sets aside a little storage location big enough to hold a
reference to a Dictionary object.  Note that it does NOT set aside
storage to hold an entire Dictionary object!  Just a reference to one. 
This is very handy, since it lets you use assignment operators with
object variables too:

  d = SomeMethodThatReturnsADictionary

When you see (or write) such an assignment, it does not mean that RB is
going to copy all the data from one object to another.  That would be
very wasteful; an object might be huge, with possible hundreds of
kilobytes of data in it.  Instead, d here is just a little 4-byte
reference that can point to a Dictionary object, and the assignment
operator simply changes what it points (refers) to.

Just as an integer variable is initially 0, an object reference is
initially Nil.  That is, it doesn't point to any object.  In theory, RB
could be designed to always initialize your object properties to a
newly minted object of that type, but this is very often not what you
want.  I may have a class that needs to keep track of what window is
using it, so I have a property declared

  owner as Window

..but this doesn't mean that I want a new window to be created as soon
as I instantiate my class!  Instead, most likely, the window of
interest will already be there, and will in fact be the thing
instantiating my little class (and then assigning it.owner = self).  So
you can see how wasteful it would be if object properties automatically
created an object of their type, instead of waiting for you to assign
them a value.

(And in fact, sometimes it wouldn't even be possible -- this is an
advanced topic, but just note that there are things called Interfaces
which define a type, like a class, and yet which can't be instantiated;
you can only instantiate other classes that implement an Interface.  So
an interface is a perfectly valid type for a property, but RB couldn't
possibly create an instance of that type for you.)

With local variables, if you want to create an instance at the same
time you declare the variable, you can easily do it with this syntax:

  Dim d as New Dictionary

This is a very common and useful idiom, that's exactly equivalent to:

  Dim d as Dictionary
  d = New Dictionary

except that it takes only one line of code.  With object properties,
there is no equivalent; you have to initialize them in the Constructor
of the class (or at some later time when you might need them to have a
certain value).

Finally, let's cover a common "gotcha" that catches newbies, especially
when it comes to dates (perhaps because dates in VB are not objects at
all, but rather an elemental type like an Integer).  If you have a date
d1, and then you want to make a related date, you might do:

  Dim d2 as Date
  d2 = d1
  d2.Day = d2.Day + 1  // set d2 to the day after d1

but this is incorrect.  You don't have two Date objects here; you have
two references to exactly the same Date object (since d2 = d1 copies
the reference from d1 into d2).  So when you modify the object referred
to by d2, you're modifying the object referred to by d1 as well -- they
are  the very same object.  To do this properly, you need to create a
new object (with New), and then somehow copy the data from the
original.  How you do this depends on the class, but with Dates you can
do:

  Dim d2 as New Date   // (note use of "New")
  d2.TotalSeconds = d1.TotalSeconds   // copy the data
  d2.Day = d2.Day + 1  // set d2 to the day after d1

since a date is completely described by its TotalSeconds value.

Questions?  You ask 'em, I'll answer 'em.

Cheers,
- Joe

--
Joe Strout -- [EMAIL PROTECTED]
Verified Express, LLC     "Making the Internet a Better Place"
http://www.verex.com/

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to