[ Pobierz całość w formacie PDF ]
.Here s the output of the Write()function: John Ashley ,33, Manager Michael Staknovitch ,24, Programmer Tess Owen ,37, Engineer Joe Dow ,28, AdministratorSeek(file_number[, position]), Loc(file_number)The Loc() function returns the current read/write position in a file.The Seek() function does thesame if called without the position argument.For a random-access file, the value returned by eitherfunction is the number of the last record read from, or written to, the file.For sequential files, thisvalue is the current byte divided by 128.For binary files, it s the number of the last byte read from,or written to, the file.If you specify the position argument of the Seek() function, you can set thecurrent read/write position in the file.To move to the beginning of the third record in a randomaccess file, use a statement like the following:Seek(fNum, 3)Lock(file_number[, fromRecord][, toRecord]),Unlock(file_number[, fromRecord][, toRecord])The Lock() function allows you to lock a file or some of the records in a random-access file.Thelocked records are not available to other applications that are currently running.Your application,however, has access to the entire file.If another application attempts to open a locked file or toaccess one of the locked records, Visual Basic will generate a runtime exception.If you omit the optional arguments, then the entire file is locked.If you specify the fromRecordargument, then all following records are locked.Finally, you can lock a range of records by specify-ing both optional arguments.Width(fNum, length)This is another useful statement that applies to sequential files only.The Width() function sets themaximum line length that can be written to a file.The maximum line length is specified by the sec-ond argument, length.A line with fewer characters than length is stored to the file as is.Longer lines arebroken; Visual Basic automatically inserts newline characters to enforce the specified maximum linelength.Use this function with the Print() and Write() functions, which append data to the sameline, to limit the length of each data line.Even better, you should use the PrintLine() and Write-Line() methods to control how much information goes to the same line.FileAttr(file_number)The FileAttr() function returns an integer representing the file mode for files opened using theFileOpen() function.The file_number argument is the number of the file.The value returned is one ofthose in Table 21.RANDOM-NUMBER GENERATION chF49Table 21: Values Returned by the FileAttr() FunctionValue Mode1 Input2Output4Random8Append32 BinaryRandom-Number GenerationVisual Basic.NET supports the random-number generator of VB6, but it also provides a class forgenerating random numbers, the System.Random class.Rnd([seed])This function returns a pseudo-random number in the range 0 to 1.The optional argument is calleda seed and is used as a starting point in the calculations that generate the random number.Note The sequence of random numbers produced by Visual Basic is always the same! Let s say you have an applicationthat displays three random numbers.If you stop and rerun the application, the same three numbers will be displayed.Thisis not a bug.It s a feature of Visual Basic that allows you to debug applications that use random numbers (if the sequencewere different, you wouldn t be able to re-create the problem).To change this default behavior, call the Randomize state-ment at the beginning of your code.This statement will initialize the random-number generator based on the value of thecomputer s Timer, and the sequences of random numbers will be different every time you run the application.If seed is negative, the Rnd() function always returns the same sequence of random numbers.Asstrange as this behavior may sound, you may need this feature to create repeatable random numbersto test your code.If seed is positive (or omitted), the Rnd() function returns the next random numberin the sequence.Finally, if seed is zero, the Rnd() function returns the most recently generated ran-dom number.In most cases, you don t need a random number between 0 and 1, but between two other integervalues.A playing card s value is an integer in the range 1 through 13.To simulate the throw of adice, you need a number in the range 1 through 6.To generate a random number in the range lowerto upper, in which both bounds are integer numbers, use the following statement:randomNumber = Int((upper - lower + 1) * Rnd() + lower)The following statement displays a random number in the range 1 to 49:Console.WriteLine(Int(Rnd() * 49 + 1))chF50 BONUS REFERENCE VB.NET FUNCTIONS AND STATEMENTSRandomize [seed]The Randomize statement initializes the random-number generator.The seed argument is a numericvalue, used to initialize the random-number generator.To create a different set of random numbersevery time the application is executed, use the current date as seed.However, using the same seed willnot return the same run of random numbers.GraphicsThis section discusses the two Visual Basic functions for color definition.The LoadPicture() andSavePicture() functions, as well as the drawing statements of VB6, are no longer supported byVB.NET.QBColor(color)This function returns an Integer representing the RGB color code corresponding to the specifiedcolor number.The color argument is a number in the range 0 through 15.Each value returns a differ-ent color, as shown in Table 22
[ Pobierz całość w formacie PDF ]