3

I was reading a block of code, which reads an Microsoft access database schema.

Between others i saw that line of code in a loop

ColName = SchemaTable.Rows(i)!COLUMN_NAME.ToString 

Why the exclamation mark is used for { ! }?

OrElse
  • 9,709
  • 39
  • 140
  • 253

2 Answers2

7

From MSDN: Special Characters in Code (Visual Basic)

Exclamation Point (!) Operator

Use the ! operator only on a class or interface as a dictionary access operator. The class or interface must have a default property that accepts a single String argument. The identifier immediately following the ! operator becomes the argument value passed to the default property as a string.

Public Class hasDefault
  Default Public ReadOnly Property index(ByVal s As String) As Integer
    Get
      Return 32768 + AscW(s)
    End Get
  End Property
End Class
Public Class testHasDefault
  Public Sub compareAccess()
    Dim hD As hasDefault = New hasDefault()
    MsgBox("Traditional access returns " & hD.index("X") & vbCrLf & 
      "Default property access returns " & hD("X") & vbCrLf & 
      "Dictionary access returns " & hD!X)
  End Sub
End Class

The three output lines of MsgBox all display the value 32856. The first line uses the traditional access to property index, the second makes use of the fact that index is the default property of class hasDefault, and the third uses dictionary access to the class.

Note that the second operand of the ! operator must be a valid Visual Basic identifier not enclosed in double quotation marks (" "). In other words, you cannot use a string literal or string variable. The following change to the last line of the MsgBox call generates an error because "X" is an enclosed string literal.

"Dictionary access returns " & hD!"X") 

References to default collections must be explicit. In particular, you cannot use the ! operator on a late-bound variable.

AakashM
  • 62,551
  • 17
  • 151
  • 186
-1

It's doing a lookup on the collection SchemaTable, looking for COLUMN_NAME.ToString, then setting that to ColName More information and examples here: http://support.microsoft.com/kb/129287

Alex
  • 2,681
  • 3
  • 28
  • 43
  • It's not doing that lookup/index on "the collection `SchemaTable`"; it's doing it on `SchemaTable.Rows(i)`. And it's not setting anything to `ColName`: it's setting `ColName` to the result of `.ToString`. Besides, the `!` has nothing to do with that. Finally, your link is dead. Even if those were fixed, this seems like a very vague answer. – underscore_d Jan 05 '18 at 19:44