I’m maintaining a set of code that has a wrapper class for SqlDataAdapter to load a System.Data.DataTable. It has a generic function to determine if the DataTable “hasRecords”. I know it’s a minor issue, but out of curiosity… Which is the faster method to use?
Existing:
Public ReadOnly Property hasRecords() As Boolean
Get
hasRecords = CBool((CBool(BOF = True) And CBool(EOF = True)) = False)
End Get
End Property
or
Posssible new:
Public ReadOnly Property hasRecords() As Boolean
Get
hasRecords = IIf(RecordCount > 0, True, False)
End Get
End Property
If RecordCount is defined as a fixed value in as a recordset property I would think the count would be faster as a single eval vs the multipart conversion/eval BOF/EOF method it is using.
Is there any other reason not to change it?
This seems like an unnecessary and potentially risky optimization; RecordCount may not always be available (see the documentation), while BOF and EOF should always return appropriate answers. So your optimization potentially breaks behavior while likely providing only a minimal return in performance (which if you are really interested in, you should test).