Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use of ! in object reference

Status
Not open for further replies.

murrayja

Programmer
May 6, 2000
90
0
0
US
I have run across something (again) that I do not understand.
The use of ! to reference a sub-object.

frm.Height = newHeight
frm!rt.Height = newHeight

'frm' is a form and 'rt' is a textbox designed on that form.
Is this equivalent to frm.rt.height = newheight?

jim murray
 
This is old syntax inhertited from older versions of VB. Will work but should use new standards
 
Old syntax? Not at all. Still in use in .NET, just badly documented. And it is not a direct alternative to the '.' operator, although on occassions it appears to give the same result.

Technically it is known as the "dictionary lookup operator.", where a dictionary is any collection type that is indexed by a key rather than a number. In other words, it is basically a shorthand for the

COM_Object.Item("index")

construct.

 
>Old syntax?

Umm, isn't it suggested by MS that it osn't used? Certainly frowned upon in ADO...

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Not exactly. Indeed, if a field in a table has the same name as an ADO method on an ADO Recordset object then you have to refer to the corresponding field in the recordset with the ! syntax to avoid an error.

Microsoft's warnings about the use of ! are not that it shouldn't be used at all, but that it shouldn't be misused, which it often is because a fair number of people don't completely understand what it does

For example put a textbox on a form, then try the following:

msgbox form1!Left
msgbox form1!text1

One generates an error, the other doesn't. Explanations on a postcard, please...

Another point that may be worth making, particularly with reference to ADO, is that the ! operator is slower than a direct reference than using an oridinal reference (i.e rs!wombat is slower than rs(0))

 
a postcard then!

currently away from development machine,
but, guessing,
form1!text1 is valid as text1 is found in the controls collection
form1!left will fail left being a form property.


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Indeed so. What each command is actually trying to do is:

form1.[_Default].Item("Text1")
form1.[_Default].Item("Left")

where the default property of a form is its Controls collection
 
It's use is probably discouraged for that reason -- people get confused about when to use it.

Plus the fact that it's super slow for ADO field access.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
>Plus the fact that it's super slow for ADO field access.

Thats probably a major factor!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top