|
|
|
|
|
|
|
|
|
|
catching errors
Catching errors is very important because often they happen when you do not expect them. The command On Error allows you to react to errors that occur within a function or procedure.
download example
Sub catchingErrors()
The code above divides one by the input. In case the user enters zero or not a number an error occurs which is caught in the nok section.
registry
When you write an application in VB you often want to save certain values and retrieve them again next time the application is started. Now instead of storing the settings in a file you can very easily add entries to the registry by use of the function SaveSetting.
download example
SaveSetting "Chess", "Properties", "Player", "white"
The code above writes the setting white to the key Player of the section Properties of the project Chess.
In order to load a certain setting from the registry use GetSetting.
col = GetSetting("Chess", "Properties", "Player", "black")
The code above retrieves the former setting. In case the key is not found the setting black is retrieved as default.
You can also delete a registry key or even a section by use of the function DeleteSetting.
DeleteSetting "Chess", "Properties", "Player"
The code above deletes the key Player from the registry.
file output
Very often there are numbers being calculated during your application that have to be stored in a file. The registry is not the right thing for this. Simply open a file and use the procedure Write# in order to write strings, numbers etc. to the file.
download example
The code below opens or generates a file called test.txt and assigns it a free handle. A text is then written to the file and at last the file is closed again.
Sub myOutput()
file input
Now that you have seen how to write numbers to a file you can read them in again. Open the file and use the procedure Input# in order to read from the file unit by unit. Just make sure you know of what datatypes and structure the file consists.
download example
Sub myInput()
binary file output
Many applications produce a lot of data that must be stored for further analysis. The registry is not the right thing for this. Simply open a file and use the procedure Print# in order to write to the file in binary mode.
download example
Sub myOutput()
binary file input
There are several ways how you can read from a file. The most flexible way is by use of the function Input in order to read in binary mode. The first parameter of Input determines how many chars are read at a time.
download example
Sub myInput()
|