Oct Functions - VB.NET



number Use: Required Data Type: Numeric or string capable of conversion to a number A valid numeric or string expression

Return Value

Returns the octal value of a given number

Rules at a Glance

  • If number is not already a whole number, it is rounded to the nearest whole number before being evaluated.
  • If number is Nothing, an error occurs.
  • Oct returns up to 11 octal characters.

Programming Tips and Gotchas
You can also use literals in your code to represent octal numbers by appending &O to the relevant octal value. For example, 100 decimal has the octal representation &O144. The following two statements assign an octal value to a variable:

On Error Statement

Syntax 1

label Use: Either label, 0, or -1 is required A valid label within the subroutine

Syntax 2
On Error Resume Next

Enables or disables error handling within a procedure.
If you don't use an On Error statement or a Try...Catch block in your procedure, or if you have explicitly switched off error handling, the Visual Basic runtime engine will automatically handle the error. First, it will display a dialog box containing the standard text of the error message, something that many users are likely to find incomprehensible. Second, it will terminate the application. So any error that occurs in the procedure will produce a fatal runtime error.

Rules at a Glance
Syntax 1

  • The 0 argument disables error handling within the procedure until the next On Error statement is executed.
  • The -1 argument disables an enabled exception in the current procedure. (It resets the exception to Nothing).
  • The label argument specifies the label that defines an error-handling routine within the current procedure. Should an error occur, the procedure will be branched to this error-handling routine.
  • A subroutine label must be suffixed with a colon. In addition, you cannot use a VB reserved word for a subroutine label name. For example:someroutine:
  • label must be in the same procedure as the On Error statement.

Syntax 2
When a runtime error occurs, program execution continues with the program line following the line that
generated the error.

Programming Tips and Gotchas

  • If you have no error handling in your procedure or if error handling is disabled, the VB runtime engine will trace back through the call stack until a procedure is reached where error handling is enabled. In that case, the error will be handled by that procedure. However, if no error handler can be found in the call stack, a runtime error occurs, and program execution is halted.
  • On Error Resume Next is useful in situations either where you are certain that errors will occur or where the errors that could occur are minor. The following example shows how you can quickly cycle through the controls on a form and set the Text property to an empty string without checking what type of control you're dealing with. Of course, you are aware that many of the controls don't have a text property, so that the attempt to access their Text property will generate an error. By using the On Error Resume Next statement, you force your program to ignore this error and carry on with the next control.
  • Use of the On Error Resume Next statement should be kept to a minimum, since errors are basically ignored and their occurrence is silent to the user. This means that, should an unexpected error (that is, an error that you were not intending to handle when you chose to ignore errors) occur or should your application behave unexpectedly, the job of finding and correcting the cause of the error becomes almost impossible.
  • The following is a template for error handling within your procedures using the On Error statement:

If cleanup code isn't required within the procedure, you can simplify the template by removing the Name_Exit label and removing the Resume Name_Exit statement.

  • If you are writing an error-handling routine for use within a class module or a DLL, you should use the following template, which raises an error back to the client, thereby notifying the client of the error and allowing the client to handle it:
  • Errors that occur within an error handler are passed up the call chain. To illustrate this, consider the following code:

When Test2 is run, the message "Error handled" is displayed. This indicates that the error that occurs in the error handler of Test is passed to Test2.

VB .NET/VB 6 Differences
In VB 6, the label in On Error GoTo label can be either a label or a line number. In VB .NET, the use of line numbers is not supported.

OpenFileDialog Class



Represents a common dialog box for selecting or opening a file. The OpenFileDialog class has properties for setting the initial appearance and functionality of the file dialog box, a property for returning the filename or names selected by the user, as well as a method for showing the dialog box. An instance of the OpenFileDialog class does not itself open the file, but instead provides the information that allows your code to do this programmatically. Under VB, the most common use for this dialog box is to get the name of a file from the user, after which we can use VB's functions to open that file.
An OpenFileDialog object can be instantiated as follows:

Dim oOpenDlg As New OpenFileDialog

Selected OpenFileDialog Members
The following is a brief description of some of the more important members of the OpenFileDialog class:

AddExtension property
Gets or sets a Boolean value that determines whether the default file extension is automatically added to the Filename property if the user fails to enter an extension. Its default value is True.

CheckFileExists property
Sets or retrieves a Boolean value indicating whether a warning message should be displayed if the user enters the name of a file that does not exist. The default value is True.

DefaultExt property
Gets or sets a String that defines the default file extension. The string should consist of the file extension only without a period.

FileName property
Returns a string that contains the fully qualified name (that is, complete path and filename) of the file selected by the user. If no file is selected, the property returns an empty string.

FileNames property
Returns a String array that contains the fully qualified names (that is, complete paths and filenames) of the files selected by the user. If no file is selected, the property returns an empty array. Note that this property returns a single-element array if the Multiselect property is False and the user selects a file.

Filter property
Gets or sets a String containing the current filter, which determines the items that appear in the "Files of type" drop-down listbox. A single item consists of a file description, a vertical bar, and the file extension (usually "*." plus the file extension). If there are multiple extensions in a single item, they are separated by semicolons. If there are multiple items, they are separated by vertical bars. For example, the following code fragment assigns a filter string to a String variable:

FilterIndex property
Gets or sets an Integer value that determines which of the items defined by the Filter property are selected. The index is one-based, rather than zero-based. When the dialog is first displayed and no FilterIndex value is specified, it defaults to 1. When the method returns, its value indicates which filter item was selected by the user.

InitialDirectory property
Gets or sets a String that defines the directory initially displayed by the OpenFileDialog dialog

Multiselect property
Sets or retrieves a Boolean value indicating whether the user is allowed to select more than one file.

OpenFile method
Opens the file selected by the user, returning a Stream object. The file is opened in read-only mode. As Microsoft puts it: "The OpenFile method is used to provide a facility to quickly open a file from the dialog box. The file is opened in read-only mode for security purposes. To open a file in a read/write mode, you must use another call " (See the "ShowDialog Method" entry.)

ReadOnlyChecked property
Sets or retrieves a Boolean value indicating whether the read-only check box is selected on the dialog box.

RestoreDirectory property
Gets or sets a Boolean value indicating whether the current directory is restored before the dialog closes. Its default value is False.

ShowDialog method
The OpenFileDialog class inherits from the FileDialog class, which in turn inherits from the commonDialog class. This class has a ShowDialog method that shows the dialog box. Once the user has dismissed the dialog box, the FileDialog's FileName and FileNames properties can be used to get the user's choice(s).

ShowReadOnly property
Sets or retrieves a Boolean value indicating whether the dialog box contains a read-only checkbox.

Title property
Gets or sets a String value containing the title of the Open dialog box.

The following code asks the user for one or more files and displays the filenames in the Output window:

VB .NET/VB 6 Differences
Whereas the OpenFileDialog class is implemented in the .NET Base Class Library, VB 6 offered the
CommonDialog custom control. Although the two offer similar functionality, their public interfaces are
almost completely different.

Option Compare Statement

Option Compare {Binary | Text}

Used to set the default method for comparing string data

Rules at a Glance

  • When Option Compare is not used in a module, the default comparison method is Binary.
  • When Option Compare is used, it must appear at the start of the module's declarations section, before any procedures.
  • Binary comparison —the default text comparison method in Visual Basic—uses the internal binary code of each character to determine the sort order of the characters. For example, "A" < "a".
  • Text comparison uses the locale settings of the current system to determine the sort order of the characters. Text comparison is case insensitive. For example, "A" = "a".

Option Explicit Statement

Option Explicit [On | Off]

Use Option Explicit to generate a compile-time error whenever a variable that has not been declared is encountered.

Rules at a Glance

  • The Option Explicit statement must appear in the declarations section of a module before any procedures.
  • In modules where the Option Explicit statement is not used, any undeclared variables are automatically cast as Objects.
  • The default is Option Explicit On. In other words, the statement:

Programming Tips and Gotchas

  • It is considered good programming practice to always use the Option Explicit statement. The following example shows why:

In this code snippet, an integer variable, iVariable, has been declared. However, because the name of the variable has been mistyped in line 3, the message box shows its value as only 50 instead of 150. This is because iVarable is assumed to be an undeclared variable whose value is 0. If the Option Explicit statement had been used, the code would not have compiled, and iVarable would have been highlighted as the cause.

  • For an ASP.NET page, you use the @ PAGE directive rather than Option Explicit to require variable declaration. Its syntax is:

By default, Explicit is true in ASP.NET pages.
You can also use the <system.web> section of the WEB.Config file to require variable declaration for an entire virtual directory or ASP.NET application by adding an explicit attribute to the compliation section. Its syntax is:

In both cases, true corresponds to Option Explicit On, and false corresponds to Option Explicit Off.

Option Strict Statement

Option Strict [On | Off]

Option Strict prevents VB from making any implicit data type conversions that are narrowing since
narrowing conversions may involve data loss. For example:

Dim lNum As Long = 2455622
Dim iNum As Integer = lNum

converts a Long (whose value can range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) to an Integer (whose value can range from 2,147,483,648 to 2,147,483,647). In this case, even though no data loss would result from the narrowing, Option Strict On would still not allow the conversion and would instead generate a compiler error. The reasoning here is that, although particular narrowing operations may not lose data, there is always the potential for data loss when working with variables—that is, with symbolic representations of numbers whose values are allowed to vary.

Rules at a Glance

  • If the Option Strict statement is not present in a module, Option Strict is Off.
  • The default is Option Strict On. In other words, the statement:
  • The Option Strict statement must appear in the declarations section of a module before any code.
  • Option Strict On disallows all implicit narrowing conversions.
  • Option Strict On also causes errors to be generated for late binding, as well as for any undeclared variables, since Option Strict On implies Option Explicit On.

Programming Tips and Gotchas

  • Although the setting of Option Strict has no effect on BCL data types, BCL data types disallow implicit narrowing conversions.
  • Explicit narrowing conversions are not affected by Option Strict. However, if data loss does occur as a result of an explicit conversion, an OverflowException exception is generated.
  • One of the most commonly overlooked narrowing conversions is the use of "wider" arguments in function, procedure, and method calls. Passing a Long to an Integer parameter, for example, is an implicit narrowing conversion that Option Strict does not allow.
  • In many cases, Option Strict On disallows seemingly "safe" conversions because it interprets literal values in unexpected ways. For example, the statement

Dim decNum As Decimal = 10.32

generates a compiler error because 10.32 is interpreted as a Double, and implicit conversions from Double to Decimal are not allowed. You can correct this compiler error with a statement like:

Dim decNum As Decimal = 10.32D

  • Setting Option Strict On is highly recommended.
  • For an ASP.NET page, you use the @ Page directive rather than Option Strict to control strict type checking. Its syntax is:

By default, Strict is false in ASP.NET pages.
You can also use the <system.web> section of the WEB.Config file to control strict type checking for an entire virtual directory or ASP.NET application by adding a strict attribute to the compilation section. Its syntax is:

In both cases, true corresponds to Option Explicit On, and false corresponds to Option Explicit Off.

VB .NET/VB 6 Differences
The Option Strict setting is new to VB .NET.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics