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 { ! }?
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.
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