Tab Function VB.NET

Class
Microsoft.VisualBasic.FileSystem

Syntax
Tab[(column)]

column

Use: Optional

Data Type: Short

A column number to which the insertion point will move before displaying or printing the next expression

Return Value
A TabInfo structure

Description
Moves the text-insertion point to a given column or to the start of the next print zone

Rules at a Glance

  • If the column argument is omitted, the text-insertion point will be moved to the beginning of the next print zone.
  • The value of column determines the behavior of the insertion point:

    TABLE

    TABLE

  • The left hand column is always 1.
  • When expressions are output to files using the Print or PrintLine statement, the width of the output is determined by the Width statement.
  • When output surface is divided into columns, the width of each column is the average width of all characters in the current point size of the current font. This means that the number of columns for tabulation purposes does not necessarily relate to the number of characters that can be printed across the width of the output surface.

Programming Tips and Gotchas
The Tab function without a column argument is useful when outputting data to a file using the Print or PrintLine statement—especially in locales where the comma would be recognized as a decimal separator.

Tan Function

Class
System.Math

Syntax

Tan(a)

a

Use: Required

Data Type: Double An angle in radians

Return Value
A Double containing the tangent of an angle
Description
Returns the ratio of two sides of a right angle triangle

Rules at a Glance

  • The returned ratio is derived by dividing the length of the side opposite the angle by the length of the side adjacent to the angle.
  • This is a Shared member, so it can be used without creating any objects.

Programming Tips and Gotchas

  • You can convert degrees to radians using the following formula:

    radians = degrees * (pi/180)

  • You can convert radians to degrees using the following formula:

    degrees = radians * (180/pi)

Tanh Function

Class
System.Math

Syntax
Math.Tanh(number)

number Use: Required Data Type: Double or numeric expression An angle in radians

Return Value
A Double denoting the hyperbolic tangent of the angle

Description
Returns the hyperbolic tangent of an angle

Rules at a Glance
This is a Shared member, so it can be used without creating any objects.

VB .NET/VB 6 Differences
Tanh is new to the .NET Framework.

Throw Statement

Syntax
[Throw exception

exception

Use: Required

Data Type: An Exception

object or an object derived from Exception An Exception object representing the exception being thrown

Description
Throws an exception that can be handled using either structured exception handling (a Try. . . Catch
block) or unstructured exception handling (the On Error statement)

VB .NET/VB 6 Differences
The Throw statement is new to VB .NET.

TimeOfDay Property

Class
Microsoft.VisualBasic.DateAndTime

Syntax
TimeOfDay
Return Value
Date value giving the current system time

Description
Sets or returns the current system time

Example

Rules at a Glance
The TimeOfDay property returns the time in the time format defined by the system's regional settings.

Programming Tips and Gotchas

  • The TimeOfDay property includes an incorrect date, 01/01/0001, along with the time. It can be eliminated with the Format or FormatDateTime function as follows:
  • Format(TimeOfDay( ), "Long Time")
    FormatDateTime(TimeOfDay( ), DateFormat.LongTime)

  • When setting the TimeOfDay property, any date component is ignored.

Timer Property

Class
Microsoft.VisualBasic.DateAndTime

Syntax
Timer

Return Value
Double representing the number of seconds that have elapsed since midnight

Description
Returns the number of seconds since midnight

Programming Tips and Gotchas

  • Timer is classified as a function in VB 6 and as a read-only property in VB .NET.
  • You can use the Timer property as an easy method of passing a seed number to the Randomize procedure, as follows:
  • Randomize Timer( )

  • The Timer property is ideal for measuring the time taken to execute a procedure or program statement, as the following snippet shows:

VB .NET/VB 6 Differences
While the Timer property returns a Double in VB .NET, the VB 6 Timer function returns a Single.

TimeSerial Function

Class
Microsoft.VisualBasic.DateAndTime

Syntax
TimeSerial(hour, minute, second)

hour

Use: Required

Data Type: Integer A number in the range 0 to 23

minute

Use: Required

Data Type: Integer Any valid integer

second

Use: Required

Data Type: Integer Any valid integer

Return Value
A Date representing the time specified by the arguments to the function

Description
Constructs a valid time given a number of hours, minutes, and seconds

Rules at a Glance

  • Any of the arguments can be specified as relative values or expressions.
  • The hour argument requires a 24-hour clock format; however, the returned time is determined by the system's regional settings.
  • If any value is greater than the normal range for the time unit to which it relates, the next higher time unit is increased accordingly. For example, a second argument of 125 will be evaluated as 2 minutes, 5 seconds.
  • If any value is less than zero, the next higher time unit is decreased accordingly. For example, TimeSerial(2,-1,30) returns 01:59:30.

Programming Tips and Gotchas
Because TimeSerial handles time units outside of their normal limits, it can be used for time calculations. However, because the DateAdd function is more flexible and is internationally aware, it should be used instead for this purpose.

TimeString Property

Class
Microsoft.VisualBasic.DateAndTime

Syntax
TimeString( )

Return Value
String representing the current system time

Description
Returns or sets the current system time

Rules at a Glance

  • The TimeString property returns the time in the format determined by the system's regional settings.
  • You can use any time format recognized by IsDate when setting the time using the TimeString property.

Programming Tips and Gotchas

  • The string returned by the TimeString property also includes an invalid date, 01/01/0001. It can be eliminated with the Format or FormatDateTime function as follows:
  • Format(TimeOfDay( ), "Long Time")
    FormatDateTime(TimeOfDay( ), DateFormat.LongTime)

  • To get or set the current system date as a String, use the DateString property.
  • To access the current system time as a Date, use the TimeOfDay property.

VB .NET/VB 6 Differences
The TimeString property is new to VB .NET.

TimeValue Function

Class
Microsoft.VisualBasic.DateAndTime

Syntax
TimeValue(stringtime)

stringtime

Use: Required Data

Type: String Any valid string representation of a time

Return Value
A Date containing the time specified by the string argument, with the date set to January 1 of the year 1

Description
Converts a string representation of a time to a Date data type

Rules at a Glance

  • If stringtime is invalid, a runtime error is generated.
  • If stringtime is Nothing, TimeValue generates an error.
  • stringtime can be in any time format recognized by the IsDate function. Both 12- and 24- hour clock formats are valid.
  • The Date value returned by time is formatted based on the system's regional settings.

Programming Tips and Gotchas

  • A time literal can also be assigned to a Date variable by surrounding the date with hash characters (#), as the following snippet demonstrates:
  • Dim dMyTime As Date
    dMyTime = #12:30:00 AM#
  • The CDate function can also cast a time expression contained within a string as a Date variable, with the additional advantage of being internationally aware.
  • The string returned by the TimeString property also includes an invalid date, 01/01/0001. It can be eliminated with the Format or FormatDateTime function as follows:

Format(TimeOfDay( ), "Long Time")
FormatDateTime(TimeOfDay( ), DateFormat.LongTime)

VB/NET/VB 6 Differences
In VB 6, TimeValue returns the time only. In VB .NET, the function also returns an invalid date,
01/01/0001, along with the time.

Today Property

Class Microsoft.VisualBasic.DateAndTime

Syntax
Today( )

Description
Sets or retrieves the current system date

Rules at a Glance

  • If you are setting the system date with numbers, as opposed to spelling the month, the sequence of Day, Month, and Year must be in the same sequence as the computer's regional settings.
  • If you are running Microsoft Windows 95, 98, or 2000, the earliest system date you can set is January 1, 1980; the latest system date you can set is December 31, 2099.
  • The date is returned in the short date format defined by the system's regional settings.

Example

Today( ) = "January 1, 1998"

Programming Tips and Gotchas

  • It is good programming practice to synchronize the dates across the machines in a multiuser environment, most commonly from the date on a server. This can be done at the operatingsystem level within the logon script or at application level using the Today property and TimeOfDay function.
  • It is risky to take a date format for granted. Wherever possible, use the Format function to explicitly set the date format that you require, prior to using a date value.
  • Modern Windows systems are more reliant on system date than ever before. A single machine can have literally hundreds of different applications installed, many of which will use dates in one way or another. You should respect the machine on which your application is running, and only in exceptional circumstances should you change the system date programmatically.

Trim Function

Class
Microsoft.VisualBasic.Strings

Syntax
Trim(str)

Str

Use: Required

Data Type: String Any string expression

Return Value
String

Description
Removes both leading and trailing spaces from a given string

Rules at a Glance
If string is Nothing, the Trim function returns Nothing.

Programming Tips and Gotchas
Trim is equivalent to calling both the RTrim and LTrim functions.

VB .NET/VB 6 Differences
In VB 6, the function's single named argument is string. In VB .NET, its single named argument is str.

Try...Catch...Finally Statement

Syntax

Description
Used to handle runtime errors.
Rules at a Glance

  • The tryStatements, which are required, constitute the Try block and are the statements that VB monitors for errors.
  • The Catchblocks, of which there can be more than one, contain code that is executed in response to VB "catching" a particular type of error within the Try block. Thus, the Catch blocks consist of the error-handlers for the Try block.
  • The phrases exception [As type] and [When expression] are referred to as filters in the VB .NET documentation. In the former case, exception is either a variable of type Exception, which is the base class that "catches" all exceptions, or a variable of one of Exception's derived classes. The When filter is typically used with user-defined errors.
  • The Exit Try statement is used to break out of any portion of a Try...Catch...Finally block.
  • The optional finallyStatements code block is executed regardless of whether an error occurs (or is caught), unless an Exit Try statement is executed.

Example
The code in the following Try block will raise an error if the user does not enter a number. The Catch
block will catch this error.

Programming Tips and Gotchas
As with unstructured error handling, VB may pass an error up the call stack when using structured error handling. This happens in the following situations:

  • If an error occurs within a Try block that is not handled by an existing Catch block.
  • If an error occurs outside any Try block (provided, of course, that no On Error-style error handlers are active).

VB .NET/VB 6 Differences
Structured exception handling using the Try...Catch...Finally construct is new to VB .NET. It replaces unstructured error handling using the On Error statement, which continues to be supported in VB .NET.

TypeName Function

Class
Microsoft.VisualBasic.Information

Syntax
TypeName(varname)

varname Use: Required Type: String literal Name of a variable

Return Value
String

Description
Returns a string giving data type information about varname. The possible return values are:

TABLE

TABLE

Rules at a Glance

  • varname returns the data type name of all value types. It returns Nothing for uninitialized reference types and the data type name for all initialized reference types.
  • If varname is an array that has been initialized or dimensioned, the returned string will be the entry in the previous table corresponding to the underlying data type of the array, but with empty parentheses appended to the end of the name. For example, if varname points to an array of integers, TypeName returns Integer( ).
  • When TypeName returns the name of a reference type, such as a class, it only returns the simple name, not the qualified name. For example, if varname points to an object of class system.Drawing.Printing.PaperSource, TypeName returns PaperSource.
  • If varname is of type Object, TypeName returns the data subtype stored to that object.

Example

Programming Tips and Gotchas
The TypeName function also works directly with members of the Foundation Class Library that aren'twrapped by Visual Basic. It reports the following data types:

TABLE

Programming Tips and Gotchas

VB .NET/VB 6 Differences

  • In VB 6, the call to the TypeName function in the code fragment:
  • Dim strVar As String
    Debug.WriteLine(TypeName(strVar))

    returns a String. In VB .NET, the TypeName function in an equivalent code fragment returns Nothing. This is because in VB .NET, strings are reference types and reference types are implemented as objects.

  • In VB 6, passing a user-defined type to the TypeName function generates a compile error. In VB .NET, it returns the name of the user-defined type or structure.
  • In VB 6, passing an uninitialized array to the TypeName function returns the type name plus parentheses. In VB .NET, it returns Nothing.
  • In VB 6, a variable whose type is not declared is reported as a Variant; in VB .NET, it is an object.

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

VB.NET Topics