LBound Function VB.NET

Class
Microsoft.VisualBasic.Information

Syntax
LBound(array[, rank])

array Use: Required Data Type: Any array

An array whose lower bound is to be determined

rank Use: Optional Data Type: Integer

The dimension whose lower bound is desired

Return Value
An Integer whose value is 0

Description
Determines the lower boundary of a specified dimension of an array. The lower boundary is the smallest subscript you can access within the specified array.

Rules at a Glance

  • Unless it is passed an invalid argument, the LBound function always returns 0.
  • If array is uninitialized, it generates an ArgumentNullException error when passed to the LBound function. You can prevent this by comparing array to Nothing, as in the following code fragment: If Not oArray Is Nothing Then
  • To determine the lower limit of the first dimension of an array, set rank to 1, set it to 2 for the second, and so on.
  • If rank isn't specified, 1 is assumed.

Programming Tips and Gotchas
Since VB .NET does not allow you to change the lower bound of an array, the LBound function wouldappear to be superfluous except for reasons of backward compatibility. Its continued use may be a good idea, though, in the event that a future version of VB .NET allows you to set the lower boundary of an array.

VB .NET/VB 6 Differences
Since VB 6 offers a number of ways to set the lower bound of all arrays or a specific array, the LBound function is particularly useful when iterating the elements of an array. In VB .NET, its use is a matter of choice.

LCase Function

Class
Microsoft.VisualBasic.Strings

Syntax
LCase(value)

value Use: Required Data Type: String or Char

A valid string expression or a character

Return Value
String or Char

Description
Converts a string to lowercase

Rules at a Glance

  • LCase only affects uppercase letters; all other characters in value are unaffected.
  • LCase returns Nothing if value contains a Nothing.
  • LCase returns the same data type as value.

Left Function

Class
Microsoft.VisualBasic.Strings

Syntax
Left(str, length)

str Use: Required Data Type: String The string to be processed length Use: Required Data Type: Long

The number of characters to return from the left of the string

Return Value
String

Description
Returns a string containing the leftmost length characters of str

Rules at a Glance

  • If length is 0, a zero-length string ("") is returned.
  • If length is greater than the length of str, str is returned.
  • If string is Nothing, Left returns Nothing.

Programming Tips and Gotchas

  • Use the Len function to determine the overall length of str.
  • The Left function corresponds to the BCL System.String class' Substring method. For example, the following two assignments to the sCity variable are functionally identical:

Note that the Substring method uses a zero-based index to determine the starting position of the substring.

Len Function

Class
Microsoft.VisualBasic.Strings

Syntax
Len(expression)

expression
Use: Required Data Type: Any

Any valid variable name or expression

Return Value
Integer

Description
Counts the number of characters within a string or the size of a given variable

Rules at a Glance

  • If expression contains Nothing, Len returns 0.
  • For a string or String variable, Len returns the number of characters in the string.
  • For a nonobject and nonstructure variable, Len returns the number of bytes required to store the variable in memory.
  • For a variable of type Object, Len returns the length of its data subtype. If the object is uninitialized, its length is 0. However, if the object contains a strongly typed class instance, an InvalidCastException exception is thrown.
  • For a structure, Len returns the number of bytes required to store the structure as a file.
  • For a strongly typed object variable, such as one defined by the Class... End Class construct, Len generates an InvalidCastException exception.
  • If varname is an array, you must also specify a valid subscript. In other words, Len cannot be used to determine the total number of elements in or the total size of an array.

Programming Tips and Gotchas

  • Len cannot accurately report the number of bytes required to store structures that contain variable-length strings. If you need to know how many bytes of storage space will be required by a structure that includes string members, you can fix the length of the strings by using the <vbFixedString(length)> attribute in the Structure statement.
  • Len is functionally similar to the BCL's System.String.Length public instance method. One significant difference is that Len retuns a 0 in the case of an uninitialized String variable, whereas the Length method raises a NullReferenceException exception. In addition, of course, other than strongly typed objects.

Like Operator

Syntax
result = string Like pattern

string Use: Required Data Type: String

The string to be tested against pattern

pattern Use: Required Data Type: String

A series of characters used by the Like operator to determine if string and pattern match

Return Type
Boolean

Description
If string matches pattern, result is True; otherwise, result is False.

Rules at a Glance

  • If either string or pattern is Nothing, then result will be Nothing.
  • The default comparison method for the Like operator is Binary. This can be overridden using the Option Compare statement.
  • Binary comparison is based on comparing the internal binary number representing each character; this produces a case-sensitive comparison.
  • Text comparison, the alternative to binary comparison, is case insensitive; therefore, A = a.
  • The sort order is based on the code page currently being used, as determined by the Windows regional settings.
  • The following table describes the special characters to use when creating a pattern; all other characters match themselves.

TABLE

special characters to use when creating a patternspecial characters to use when creating a pattern

  • list is used to match a group of characters in pattern to a single character in string and can contain almost all available characters, including digits.
  • Use a hyphen (-) in list to create a range of characters to match a character in string. For example, [A-D] will match A, B, C, or D in that character position in string.
  • Multiple ranges of characters can be included in list without the use of a delimiter. For example, [A-D J-L].
  • Ranges of characters should appear in sort order. For example, [c-k].
  • Use the hyphen at the start or end of list to match to itself. For example, [- A-G] matches a hyphen or any character from A to G.
  • The exclamation point in pattern matching is like the negation operator in C. Use an exclamation point before a character or range of characters in list to match all but that character. For example, [!A-G] matches all characters apart from the characters from A to G.
  • The exclamation point outside of the bracket matches itself.
  • To use any special character as a matching character, you should enclose the special character in brackets. For example, to match to a question mark, use [?].

Example
The following example will display OK if the text entered into Text1 starts with either V or A, followed by any characters, and ends with "in a Nutshell." Therefore, "Paul in a Nutshell" returns Wrong, whereaseither "ASP in a Nutshell" or "VB .NET Language in a Nutshell" returns OK.

Programming Tips and Gotchas

  • Different languages place different priority on particular characters with relation to sort order. Therefore, the same program using the same data may yield different results when run on machines in different parts of the world, depending upon the locale settings of the systems.
  • Regular expressions provide an even more powerful method for searching and comparing strings. You can use regular expressions through the .NET Framework's System.Text.RegularExpressions.RegEx class.

LineInput Function

Class
Microsoft.VisualBasic.FileSystem

Syntax
LineInput(filenumber)

filenumber Use: Required Data Type: Integer

Any valid file number

Return Value
A String containing the line read from the file

Description
Assigns a single line from a sequential file opened in Input mode to a string variable

Rules at a Glance

  • Data is read into a buffer one character at a time until a line feed or carriage-return sequence (either Chr(13) or Chr(13)+Chr(10)) is encountered. When this happens, all the characters in the buffer are returned as a string, without the carriage-return sequence, and the buffer is cleared.
  • After reading a line, the file pointer advances to the first character after the end of the line or to the end-of-file marker.

Example
The following code reads all of the lines in a text file and sends them to the Output window:

Programming Tips and Gotchas
You use the LineInput function to read data from text files. To write data back to this type of file, use the PrintLine function.

VB .NET/VB 6 Differences
The VB .NET LineInput function corresponds directly to the VB 6 LineInput statement, with the following differences.

  • The VB 6 LineInput statement has a second argument, varname, which is the variable to receive the line read by the function. It is not supported by the VB .NET LineInput function, since the line read is the return value of the function.
  • The first argument of the VB 6 LineInput statement, filenumber, could be preceded by the # symbol. In VB .NET, this format is not supported.

Loc Function

Class
Microsoft.VisualBasic.FileSystem

Syntax
Loc(filenumber)

filenumber Use: RequiredDim fr As Integer = FreeFile( ) Data Type: Integer

Any valid file number

Return Value
A Long indicating the current position of the read/write pointer in a file

Description
Determines the current position of the file read/write pointer

Rules at a Glance

  • If you have opened the file in Random mode, Loc returns the record number of the last record read or written.
  • If you have opened the file in Input or Output modes (sequential), Loc returns the current byte position in the file divided by 128.
  • If you have opened the file in Binary mode, Loc returns the position of the last byte read or written.

Example

Programming Tips and Gotchas

  • For sequential files, the return value of Loc is not required and should not be used.
  • Note that you cannot set the position of the file pointer using Loc.

Lock Procedure

Class
Microsoft.VisualBasic.FileSystem

Syntax

Lock(filenumber[, record] or: Lock(filenumber[, fromrecord,torecord] filenumber Use: Required Data Type: Integer

Any valid file number

record Use: Optional Data Type: Long

The record or byte number at which to commence the lock

fromrecord Use: Optional Data Type: Long

The first record or byte number to lock

torecord Use: Optional Data Type: Long

The last record or byte number to lock

Description
The Lock procedure prevents another process from accessing a record, section, or whole file until it isunlocked by the Unlock function.Use the Lock procedure in situations where multiple programs or more than one instance of your program may need read and write access to the same data file.

Rules at a Glance

  • Use the Lock procedure with only the filenumber argument to lock the whole file.
  • record is interpreted as a record number in the case of random files and a byte number in the case of binary files. Records and bytes in a file are always numbered sequentially from 1 onward.
  • To lock a particular record, specify its record number as record, and only that record will be locked.
  • The Lock procedure locks an entire file opened in Input or Output (sequential) mode, regardless of the record argument.
  • If you omit the start argument, Lock will lock all records from the start of the file to record or byte number end.
  • Attempting to access a locked file or portion of a file returns runtime error 70, "Permission denied."

Programming Tips and Gotchas

  • You must take care to remove all file locks with the Unlock procedure before either closing a file or ending the application; otherwise, you can leave the file in an unstable state. This of course means that, where appropriate, your error-handling routines must be made aware of any locks you currently have in place so that they may be removed if necessary.
  • You use the Lock and Unlock procedures in pairs, and the argument lists of both statements must match exactly.
  • The Lock procedure does not guarantee under all circumstances that the locked file will be protected from access by other processes. There are two major circumstances under which an apparent access violation can occur:

The file has already been opened but has not been locked by a process when the current process locks it. However, the first process will not be able to perform operations on the file once the second file successfully locks it.
The block of code responsible for opening the file and then locking it is interrupted by the scheduling policy of the operating system before the file can be locked. If a second process then opens and locks the file, it—and not the first process—will have sole use of the file.

Because of this, the Lock procedure should immediately follow the FileOpen procedure in code. This reduces, but does not eliminate, the problems that result from the fact that opening and locking a file is not an atomic operation.

VB .NET/VB 6 Differences
In the VB 6 Lock statement, you can separate the fromrecord and torecord arguments with the To keyword. In the VB .NET Lock procedure, this syntax is not supported.

LOF Function

Class
Microsoft.VisualBasic.FileSystem
Syntax
LOF(filenumber)

filenumber Use: Required Data Type: Integer

Any valid file number

Return Value
Long Integer

Description
Returns the size of an open file in bytes

Rules at a Glance
filenumber must be the number of a file opened using the FileOpenfunction.

Example
The following example shows how to use the LOF function to prevent reading past the end of a file in binary mode:

Programming Tips and Gotchas
LOF works only on an open file; if you need to know the size of a file that isn't open, use the FileLen function.

Log Function

Class
System.Math

Syntax Math.Log(d) or: Math.Log(a, newbase)
d or a
Use: Required Data Type: Double

A numeric expression greater than zero

newbase Use: Required Data Type: Double

The base of the logarithm

Return Value
Double

Description
Returns the natural (base e) logarithm of a given number (the first syntax) or the logarithm of a givennumber in a specified base (the second syntax)

Rules at a Glance

  • The natural logarithm is the logarithm base e, a constant whose value is approximately 2.718282. The natural logarithm satisfies the equation:

    e^Log(x) = x

    In other words, the natural logarithm function is the inverse function of the exponential function.

  • d or a, the value whose natural logarithm the function is to return, must be a positive real number. If number is negative or zero, the function generates runtime error 5, "Invalid procedure call or argument."
  • This is a Shared member, so it can be used without creating any objects.

Programming Tips and Gotchas

  • You can calculate base-n logarithms for any number, x, by dividing the natural logarithm of x by the natural logarithm of n, as the following expression illustrates:

    For example, the Log10 function shows the source code for a custom function that calculates base-10 logarithms:

  • The inverse trigonometric functions, which are not intrinsic to VB, can be computed using the value returned by the Log function. The functions and their formulas are:

Log10 Function

Class
System.Math

Syntax
Math.Log10(d)
d

Use: Required Data Type: Double

A numeric expression greater than zero

Return Value
Double

Description
Returns the common (base-10) logarithm of a given number

Rules at a Glance

  • The common logarithm is the logarithm base-10. The common logarithm satisfies the equation:
    10^Log10(x) = x
  • d, the value whose common logarithm the function is to return, must be a positive real number. If number is negative or zero, the function generates runtime error 5, "Invalid procedure call or argument."
  • This is a Shared member, so it can be used without creating any objects.

VB .NET/VB 6 Differences
The Log10 function is new to the .NET platform.

LTrim Function

Class
Microsoft.VisualBasic.Strings

Syntax

LTrim(str)
str

Use: Required Data Type: String

A valid string expression

Return Value
String

Description
Removes any leading spaces from str

Rules at a Glance

  • If str has no leading spaces, the function returns str unmodified.
  • If str is Nothing, LTrim returns Nothing.

Programming Tips and Gotchas

It is unwise to create data relationships that rely on leading spaces, especially since most string-baseddata types in relational database-management systems (like SQL Server and Access) automatically remove leading spaces.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

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

VB.NET Topics