The Open() method of class VLShape will be used to open a file, and set it to a vector layer. The method will be defined to take a variable number of arguments, as indicated by the VARARGS keyword, either two or three. The first two arguments will be the file name and layer number respectively. The third argument, if given, will be the access specifier. If a third argument is not given, read-only access will be initiated.
define method Open( filename, layer, varargs ) on VLShape
try
!
! determine file access
!
local string access
if (f$len(varargs) = 0) then
access = "r"
else
access = varargs(1)
endif
!
! attempt to open the file
!
this.fd = DBOpen( filename, access )
if ( this.fd = 0 ) call EASIError( 700 )
this.filename = filename
this.access = access
this.layer = layer
!
! Initialize shape id to the first shape in the layer
!
call this.First()
!
! read the number of fields and the fieldnames
!
this.numfields = VECGetFieldCount( this.fd, this.layer )
local integer i
for i = 1 to this.numfields
this.fieldnames[i] = VECGetFieldName( this.fd, \
this.layer, i )
endfor
onerror
!
! if an error occurred, close the file
!
call this.Close()
call EASIError( 700, "File could not be opened" )
endonerror
enddefine
Observe that the body of the Open() method is enclosed in a TRY statement to trap errors. Since a call to DBOpen() returns a value of 0 if the file could not be opened, instead of raising an EASI error, an explicit call to EASIError() is made. This allows the TRY statement to handle the error just as any other. Since the error will never propagate out of the function, no error message is required.
The Close() method will close the file if one is open.
define method Close() on VLShape
if ( this.fd != 0 ) then
call DBClose( this.fd )
this.fd = 0
endif
enddefine
© PCI Geomatics Enterprises, Inc.®, 2026. All rights reserved.