What's the difference between objRS("name") and objRS.Fields("name")?
In an effort to prove that I do occasionally manage
to wade through all the spam and read a message from our
users, I figured I'd tackle one I received the other day.
Here's the question...
Actually, this is a humble request for you to write an
article! I often see the following references to table
values. Please explain the differences (not that I see
any), as well as which is preferred / best.
For the most part, you're right... In VBScript
there is almost no difference.
The reason behind the confusion is what are called
default properties. Many objects have a
property that is the default returned when a specific
one is not asked for. Using the example above...
The default property of an Item object in the Fields collection is it's Value.
The default property of the Fields collection is the Item object.
The default property of an ADO recordset object is it's Fields collection.
As a result... you can leave all the defaults out
and your script runs the same as if you had
explicitly specified them. While default
properties can make code easier to write, they
also have several drawbacks, the most problematic
of which is often readability by other people who
are not familiar with the objects being used. Sure
most programmers will figure out an ADO recordset, but
what about those custom business logic components?
So as to which format is best... in general it's
better to spell out exactly what you mean. It helps
avoid confusion and you'll have fewer problem when
you upgrade/switch to VB.NET. That being said... if
you look at my code you'll tend to see this format:
It always seemed the most reasonable (and readable) to me...
or then again maybe I'm just lazy. ;) I
guess this is one of those "do as I say and
not as I do" scenarios.