Fog Creek Software
Discussion Board

Implicit References in VB

I'm playing around with VB again (its like a scab that I can't help picking at], and I've run into a question for you VB long timers out there.

Most of my code for the past 6 months has used explicit references vs. the implicit referencing you typically find in VBA married to Access. On my new project, I inherited legacy code that uses lots of implicit referencing [exclamation point referencing, for those of you not in the know: Form!SomeControl.Value]. Now, I see on MSDN (specific to Form.Controls in Access) that implicit referencing is faster. My question is: Why? Does it have something to do with compiler optimization (the system has more free reign?) Does this scale to all references or simply Access references?

Dustin Alexander
Thursday, July 24, 2003

Interesting.  A couple clarifying questions:

1) Where did you see that on MSDN?
2) How much faster are we talking about here?  Taking 20 nanoseconds off of the call overhead of a 10 billion nanosecond database access is "faster", but I wouldn't use it as a selling point.

I am always suspicious of articles which say "do it this way, it's faster", because what they mean is "it is faster if this particular thing happens to be the gating performance factor, which it probably is not." 

Forgive the obvious advice, but if you want improved speed, I'd spend some time optimizing database queries, improving database design, adding indices, adding stored procedures, etc.  You're likely to get more bang for the buck.


Eric Lippert
Thursday, July 24, 2003

My bible on MS Access, the Getz/Litwin/Gilbert v.1 of Access 2000 Developer's Handbook says:

"It turns out that, behind the scenes, the former style of dot-and-bang reference [what you call 'implicit' referencing] is translated to the latter style of parentheses and quotes reference [your 'explicit' referencing] when you execute such a statement.  This means that, although using the bang operator will save you a bit of typing, you'll pay for it in a speed penalty."

So I'm a bit surprised that something on MSDN would say the opposite.  I generally take ADH to be the final word Access. 

In any case, if there is a speed penalty either way it's a small one.  Not something I'd pay much attention to unless you're using it in a many-interation loop where you need to optimize performance.

The more important factor that would sway me to use the 'explicit' or 'parentheses and quotes' style of reference is that in it you can use a string variable to alter the object of the reference, whereas with the dot-and-bang reference style things are hard-coded in, there's no way to use variables to vary the reference.

I would be interested in reading the MSDN article you're referring to, though.

Herbert Sitz
Thursday, July 24, 2003

OK Eric, consider the advice forgotten. On the references, how much faster is the exact question I had. I too was skeptical, hence my initial post. As for where I saw this, it is now eluding my careful searches. Somewhere within the bowels of Access 97 documentation about the Form.Controls collection.

It also turns out that implicit referencing covers the default property [such as .Value] of a property. More on this, following, where it says it 'may' be more efficient:

And I read that translation from ! -> . occurs at compile time in VB4 in yet another MSDN article I have misplaced. Things that make you say to yourself: old was that article I was reading?

I agree with you that the string is the design way to go. I was just interested in the magnitude of the performance delta (which is obviously near trivial, regardless).

Dustin Alexander
Thursday, July 24, 2003

That explains it.  Look more closely at the article and you'll see that the distinction between implicit and explicit reference is different from the distinction between 'dot-and-bang' references and 'parentheses-and-quote' references.

Your finding something on this in the Access 97 help also indicates this, since I don't believe the 'parentheses-and-quote' style of reference even existed until Access 2k.

I believe you can do both implicit or explicit style referencing with either of the two reference styles, 'dot-and-bang' or 'parenthese-and-quote'.

Herbert Sitz
Thursday, July 24, 2003

You are not going to notice the performance difference for most code.

If you have a huge loop, then shove the referance into a seperate field

dim  lngTheField    as dao.Field

set lngTheField = me!LastName

As for brining up the issue of option explicit? That is a different issue, but anyone who tries to develop code without option explicit should be carried away! A97 default with option explicit. For some bizarre reason, you have to go into the tools settings to make this the default for a2000 and later. (dumb default!). That is the FIRST setting I would change when using a2000/a2002.

As for dot vs bang in forms code?

If you use the dot notion, and you change (or set) the recordsouce of a form at runtime, then the dot notion ONLY WORKS if a control is on the form.  A compile error will in fact occur if the forms record source is not set, and you use a .dot notation with a field that is NOT placed on the form.
This does give you some compile time checking.

However, using the Bang “!” Notation is better IF YOU ARE setting the forms record source at runtime. Further, if you delete a text box from the form, then the bang notation will STILL WORK. So, the reason why bang is better because you can reference ANY FIELD in the forms record source and a control DOES NOT have to be placed on the form. As mentioned, this problem does NOT occur if you don’t change the forms recordsouce. In fact, if the form does NOT have a record source at design time, then you MUST use the “!”.

Since I don’t want to care, or often don’t want to place a text box on the screen to get a the forms record source data, then I use the ! notation. You loose the compile time checking, but gain not having to place text boxes on the form to get at data.

Further, many professional access developers ALWAYS change the name of their textboxes on a form to something different then the fields underlying table.  I am guilty, and don’t bother changing the text box name,  but I would not fault anyone who takes this extra step.

The “rule” as to when to use “dot” vs “bang” is supposed to be “access” defined collections you use (dot) and for you own defines (bang). Of course, the controls collection on a form is actually your it is still a bit gray, and using .dot gives you inteli sense.

Albert D. Kallal  (Microsoft MVP – ms-access)
Edmonton, Alberta Canada

Albert D. Kallal
Thursday, July 24, 2003

This thread has gotten a bit confused.  Dustin originally posted that he had read that implicit referencing was slower than explicit referencing.  This use of 'explicit' and 'implicit' has nothing to do with the 'Option Explicit' in VB modules that requires you to declare a variable before it's used.

Dustin characterized the implicit/explicit reference distinction as the same as the dot-and-bang vs. parentheses-and-quotes reference styles.  This was a mistake; the two sets are different.  To illustrate this, here are two example using parentheses-and-quotes style reference.  The first makes an explicit reference to a control on a second-level-down subform, the second makes an implicit reference to the same control (which basically means it makes use of the idea of "default collections" that don't need to be explicitly named):

Explicit reference using parentheses-and-quotes:

blnAlive = Forms("frmMain").Controls("frmSub1").Form.Controls("frmSub2").Form.Controls("ckAlive")

The same control referenced implicitly:

blnAlive = Forms("frmMain")("frmSub1")("frmSub2")("ckAlive")

The implicit reference takes advantage of the fact that the Controls collection is the default collection for a form, so if you don't reference a collection in a form reference, Access will automatically choose the Controls collection.

The same control can be referenced both explictly and implicitly using the older dot-and-bang style of reference, but I'll leave that as an exercise for the reader.

Herbert Sitz
Thursday, July 24, 2003

Interesting information, all. Thanks for the details. I think the real fact that we have discovered here is that MSDN documentation is, in fact, deliberately obfuscated to make our lives more difficult :)

For instance, if I could find that article, you would see that it did indeed refer to bang references as implicit referencing, although the thinking man would wonder how it was implicit if you have to put a name in anyway.

Regardless, I think we've gotten far enough down the path to realize that any performance gain touted in the aforementioned lost and moderately inaccurate documentation is non-existent or miniscule at best.

This was an interesting language design choice on Microsoft's part. It seems to me that including three ways of referencing a contained variable in a language might tend towards the confusing, regardless of the minor gains in coding efficiency.

Dustin Alexander
Friday, July 25, 2003

If the article you cant' find was written for Access 97, then I think the explanation is that it was only pointing out _a particular_ bang reference that was implicit, not that all bang references were implicit.  Especially since the only type of reference style that existed in Access 97 was the bang style.  The parentheses-and-quote style didn't exist and wasn't available to contrast with the bang style.

For example:

The bang style reference: Form!textedit1

implicitly references the textedit1 control.

The bang style reference:  Form.Controls!textedit1

explicitly references the same value of textedit1.

It's not the name of the object itself (i.e., 'textedit1') that is explicit or implicit, but rather the naming of the collection holding the referenced object.  In this case, the Controls collection can be referenced implicitly in the first version because if you don't specify it Access will put it in for you because the Controls collection is the default collection for a Form.

The general rule for dot-and-bang style reference is that a bang (!) operator separates an object from the collection it's in, while the dot (.) operator separates an object from a property, method, or collection of that object.  The implicit style of reference allows you to violate this general rule, since, e.g., in Form!txtEdit1 the bang is used to separate txtEdit1 not from the collection that it's in (i.e., Controls), but from the Form holding the Controls collection.

Herbert Sitz
Friday, July 25, 2003

Paul Vick put an aritcle up on how VB.NET handles the ! operator on his blog a few days ago.

Kind of interesting, and something that a VB.NET nut like me ought to have known...

Dave Rothgery
Friday, July 25, 2003

*  Recent Topics

*  Fog Creek Home