Jump to content

Existence detection

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Magog the Ogre (talk | contribs) at 19:24, 4 December 2010 ({{comp-sci-stub}}). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Existence checking or existence detection is an important aspect of many computer programs. An existence check before reading a file can catch and/or prevent a fatal error, for instance. For that reasons, most programming language libraries contain File.Exists methods.

An existence check can sometimes involve a "brute force" approach of checking all records for a given identifier, as in this Microsoft Excel Visual Basic for Applications code for detecting whether a worksheet exists:

Function SheetExists(sheetName As String) As Boolean

Dim  sheetCount As Integer
Dim t As Integer

SheetExists =  False
sheetCount = ActiveWorkbook.Sheets.Count
For t = 1 To  sheetCount
If Sheets(t).Name = sheetName Then
SheetExists = True
Exit Function
End If
Next t
End Function

References