[ Pobierz całość w formacie PDF ]
." If you assign a floating point value to constantexpression, it s roundedand converted to a long integer automatically." If constantexpression is omitted, the value assigned to membername is 0 ifit s the first expression in the enumeration.Otherwise, its value is 1 greaterthan the value of the preceding membername." The values assigned to membername can t be modified at runtime.Programming Tips & Gotchas" When two enumerated data types in different libraries share the same namebut different members or values, references to the enumerated type areresolved based on the library that has been assigned the highest priority inthe References dialog.Because enumerated data types aren t members of aclass (even though they are defined in a class s type library), a reference to aparticular enumerated data type can t be qualified with a class name or libraryname." Once you define an enumerated type, you can use name as the return valueof a function.For example, given the enumeration:Public Enum enQuarterenQ1 = 1enQ2 = 2enQ3 = 3enQ4 = 4End Enumyou can use it as the return value of a function, as illustrated by the followingfunction declaration:Public Function QuarterFromDate(datVar as Date) _As enQuarterYou can also use it in a procedure s parameter list when defining a parame-ter s data type, as in the following code fragment:Public Function GetQuarterlySales(intQ As enQuarter) _As DoubleEnum Statement 247VB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.The Language Reference" Individual values of an enumerated type can be used in your program justlike normal constants." Enumerated types provide the advantage of allowing you to replace numericvalues with more mnemonic labels, and of allowing you to select values usingthe Auto List Members feature in the VB and VBA IDEs.However, when youdeclare a parameter or function s return value to be an enumerated type, VBAprovides no automatic type checking.Consequently, if your program needs toinsure that the values passed to a routine are valid, your code is responsiblefor handling the validation." Remember that the members of an enumerated type must evaluate to a Long.This is a major limitation, and there is no workaround.See AlsoConst Statement, Select Case StatementEnviron, Environ$ FunctionsNamed ArgumentsYesSyntaxEnviron({envstring | number})envstringKey: OptionalData Type: StringThe name of the required environment variable.numberKey: OptionalData Type: Numeric expressionThe ordinal number of the environment variable within the environmentstring table.Return ValueEnviron returns a string containing the text assigned to envstring.DescriptionReturns the value assigned to an operating-system environment variable.Rules at a Glance" A zero-length string ("") is returned if envstring doesn t exist in the operat-ing system s environment-string table, or if there is no environment string inthe position specified by number." envstring and number are mutually exclusive; that is, you can specify oneor the other, but not both.248 Chapter 7 The Language ReferenceVB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.EOF FunctionExamplePrivate Type envstrVarName As StringstrValue As StringEnd TypePrivate Sub Form_Load()Dim intCtr As Integer, intPos As IntegerDim strRetVal As StringDim udtEnv As envintCtr = 1DostrRetVal = Environ(intCtr)If strRetVal "" ThenintPos = InStr(1, strRetVal, "=")udtEnv.strVarName = Left(strRetVal, intPos - 1)udtEnv.strValue = Mid(strRetVal, intPos + 1)ElseExit DoEnd IfintCtr = intCtr + 1LoopEnd SubProgramming Tips & Gotchas" If you use number to specify the environment variable, both the name andthe value of the variable are returned.An equals sign (=) separates them.Forexample, the function call Environ(1) might return the string TEMP=C:\WIN-DOWS\TEMP." If you retrieve environment variables and their values by ordinal position, thefirst variable is in position 1, not position 0." Due to the flexibility offered, it s now accepted and recommended practice touse the registry for variables needed by your application, rather than the envi-ronment string table." Environment variables can be defined in a variety of ways, including by theAUTOEXEC.BAT and MSDOS.SYS files, as well as by the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SessionManager\Envi-ronment and HKEY_CURRENT_USER\Environment keys in the registry.How-ever, the Environ function doesn t recognize environment variables defined inthe registry.EOF FunctionNamed ArgumentsNoEOF Function 249VB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.The Language ReferenceSyntaxEOF(filenumber)filenumberUse: RequiredData: IntegerAny valid file number.Return ValueAn integer containing 1 (True), or 0 (False).DescriptionReturns an integer evaluating to True ( 1) when the end of a file has beenreached; until the end of the file is reached, EOF returns False (0).Rules at a Glance" filenumber must be a valid number used in the Open statement to openeither a random or sequential file." If you have opened the file using either random or binary access, a Get state-ment that can t read a complete record (i.e., an attempt to access a record pastthe last record in the file) causes EOF to return True.ExampleiFile = FreeFileOpen sFilename for Input as #iFileDo While Not EOF(iFile)LineInput #iFile, sDataLine.LoopClose #iFileProgramming Tips & Gotchas" EOF allows you to test whether the end of a file has been reached withoutgenerating an error." Because EOF dates back to the times when VB didn t support an intrinsicBoolean data type, the function uses an integer data type to hold the 0 and 1False and True values
[ Pobierz całość w formacie PDF ]