Thứ Hai, 17 tháng 8, 2009

VB.NET Functions and Statements Part I

VB.NET Functions and Statements

This bonus reference describes the functions and statements that are supported by Visual Basic .NET, grouped by category. When you’re searching for the statement to open a file, you probably want to locate all file I/O commands in one place. This is exactly how this reference is organized. Moreover, by grouping all related functions and statements in one place, I can present examples that combine more than one function or statement.
The majority of the functions are the same as in VB6. One difference is that many of the VB6 statements are implemented as functions in VB.NET. Moreover, many VB6 functions have an equivalent method in a Framework class. VB programmers are so accustomed to the old functions that they will not consider the alternatives—at least for a while. The Len() function of VB6 returns the length of a string. In VB.NET you can retrieve the length of a string with the Length method of a string variable. If strVar is declared as string variable, you can retrieve its length by calling the Length method:

Dim strVar As String = “a short string”
Console.WriteLine(“The string contains “ & strVar.Length & “ characters”)
Or you can call the Len() function passing the name of the string as argument:
Dim strVar As String = “a short string”
Console.WriteLine(“The string contains “ & Len(strVar) & “ characters”)

Most of the built-in functions are VB6 functions, and they accept optional arguments.
VB.NET uses overloaded forms of the same function, and this is an important difference you
have to keep in mind as you work with the built-in functions. If you omit an optional argument,
you must still insert the comma to indicate that an argument is missing. Optional arguments are
enclosed in square brackets. The Mid() function, for example, extracts a number of characters
from a string, and its syntax is

newString = Mid(string[, start][, length])

The starting location of the characters to be extracted is specified by the start argument, and the number of characters to be extracted is length. If you omit the start argument, the extraction starts with the first character in the string. If you omit the length argument, all the characters from the specified position to the end of the string are extracted. The only mandatory argument is the first one, which is the string from which the characters will be extracted, and this argument can’t be omitted.
The methods of the various classes are discussed in detail in the book. This bonus reference contains all the functions supported by VB.NET, and these functions are listed by category in Table 1.
Items in the table that are not followed by parentheses are statements and are also described in this reference.

Table 1: VB.NET Functions by Type
Type Functions
Input/Output InputBox(), MsgBox()
File and Folder Manipulation ChDir(), ChDrive(), CurDir(), Dir(), FileCopy(), FileDateTime(), FileLen,
GetAttr(), Kill, MkDir(), Rename(), RmDir(), SetAttr()
Data Type Identification IsArray(), IsDate(), IsDBNull(), IsNothing() IsNumeric(), IsReference,
TypeName(), VarType()
Variable Type Conversion CBool(), CByte(), CChar(), CDate(), CDbl(), CDec(), CInt(), CLng(), CObj(),

CShort(), CSng(), CStr(), CType()
String Manipulation Asc(), AscW(), Chr(), ChrW(), Filter(), InStr(), InStrRev(), Join(), LCase(),

Left(), Len(), LTrim(), Mid(), Mid, Replace(), Right(), RTrim(), Space(),

Split(), StrComp(), StrConv(), StrDup(), StrReverse(), Trim(), UCase()
Data Formatting Format(), FormatCurrency(), FormatDateTime(), FormatNumber(),

FormatPercent(), LSet(), RSet(), Str(), Val()
Math Abs(), Atan(), Cos(), Exp(), Fix(), Hex(), Int(), Log(), Oct(), Pow(),

Round(), Sin(), Sqrt(), Tan()
Date and Time DateAdd(), DateDiff(), DatePart(), DateSerial(), DateValue(), Day(),

Hour(), Minute(), Month(), MonthName(), Now(), Second(),

TimeSerial(), TimeValue(), Weekday(), WeekdayName(), Year()
Financial DDB(), FV(), IPmt(), IRR(), MIRR(), NPer(), NPV(), Pmt(), PPmt(), PV(),

Rate(), SLN(), SYD()
File I/O EOF(), FileAttr(), FileClose(), FileOpen(), FileGet(), FilePut(), FreeFile(),

Input(), LineInput(), Loc(), Lock(), LOF(), Print(), PrintLine(), Reset(),

Seek(), Unlock(), Width(), Write(), WriteLine()
Random Numbers Rnd(), Randomize
Graphics QBColor(), RGB()
Registry DeleteSetting(), GetAllSettings(), GetSetting(), SaveSetting()
Application Collaboration AppActivate(), Shell()
Miscellaneous Beep, CallByName(), Choose(), Environ(), IIf(), Option, Switch()

These functions and statements are described in the following sections, along with examples. The entries are not discussed alphabetically within each category. I start with the simpler ones so that I can present examples that combine more than one function and/or statement.
To be continued.

Không có nhận xét nào:

Đăng nhận xét