Section 7.1. The System Namespace


7.1. The System Namespace

The System namespace contains classes for such wide-ranging features as:

  • Data types

  • Data-type conversions

  • Events and event handlers

  • Mathematics

  • Program invocation

  • Application-environment management

It is also the root for almost every other significant Microsoft-supplied .NET class and namespace.

7.1.1. The System.Convert Class

The System namespace defines a class called Convert, which implements various data conversion methods. One such method is ToBoolean, which includes the following usage variations:

     Overloads Public Shared Function ToBoolean(Boolean) As Boolean     Overloads Public Shared Function ToBoolean(Byte) As Boolean     Overloads Public Shared Function ToBoolean(Char) As Boolean     Overloads Public Shared Function ToBoolean(DateTime) As Boolean     Overloads Public Shared Function ToBoolean(Decimal) As Boolean     Overloads Public Shared Function ToBoolean(Double) As Boolean     Overloads Public Shared Function ToBoolean(Integer) As Boolean     Overloads Public Shared Function ToBoolean(Long) As Boolean     Overloads Public Shared Function ToBoolean(Object) As Boolean     Overloads Public Shared Function ToBoolean(SByte) As Boolean     Overloads Public Shared Function ToBoolean(Short) As Boolean     Overloads Public Shared Function ToBoolean(Single) As Boolean     Overloads Public Shared Function ToBoolean(String) As Boolean     Overloads Public Shared Function ToBoolean(UInt16) As Boolean     Overloads Public Shared Function ToBoolean(UInt32) As Boolean     Overloads Public Shared Function ToBoolean(UInt64) As Boolean 

As you can see, there are many ToBoolean functionseach one overloaded with a different argument signatureto take care of converting various data types to a Boolean.

Now, just for exercise, consider this block of code:

     Dim textVersion As String     Dim trueVersion As Boolean     textVersion = "false"     trueVersion = System.Convert.ToBoolean(textVersion)     MsgBox(trueVersion)   ' Displays "False" 

Because the System namespace is always available (or if we are programming outside of Visual Studio, we can import it using the Imports statement), we can omit the System qualifier and write:

     trueVersion = Convert.ToBoolean(textVersion) 

The built-in VB function CBool also performs this conversion.

The Convert class contains methods for converting data to the standard Visual Basic data types, as well as to other data types supported by the .NET Framework but not wrapped by Visual Basic. (New in 2005. Beginning in the 2005 release, Visual Basic now includes native implementations of all core .NET data types.) The most important of these methods are shown in Table 7-1.

Table 7-1. Members of the System.Convert class

Method

VB equivalent

Description

ToBoolean

CBool

Converts a value to Boolean

ToByte

CByte

Converts a value to an unsigned 8-bit integer Byte

ToChar

CChar

Converts a value to a single character Char

ToDateTime

CDate

Converts a value to date or time value DateTime (Date in Visual Basic)

ToDecimal

CDec

Converts a value to a floating point Decimal

ToDouble

CDbl

Converts a value to a floating point Double

ToInt16

CShort

Converts a value to a signed 16-bit integer Int16 (Short in Visual Basic)

ToInt32

CInt

Converts a value to a signed 32-bit integer Int32 (Integer in Visual Basic)

ToInt64

CLng

Converts a value to a signed 64-bit integer Int64 (Long in Visual Basic)

ToSByte

New in 2005: CSByte

Converts a value to a signed 8-bit integer SByte (New in 2005: SByte in Visual Basic)

ToSingle

CSng

Converts a value to a floating point Single

ToString

CStr

Converts a value to a character String

ToUInt16

New in 2005: CUShort

Converts a value to an unsigned 16-bit integer UInt16 (New in 2005: UShort in Visual Basic)

ToUInt32

New in 2005: CUInt

Converts a value to an unsigned 32-bit integer UInt32 (New in 2005: UInteger in Visual Basic)

ToUInt64

New in 2005: CULng

Converts a value to an unsigned 64-bit integer UInt64 (New in 2005: ULong in Visual Basic)


7.1.2. The System.Array Class

The System.Array class contains useful methods for dealing with arrays. For instance, it has a Sort method that sorts the elements of an array. The following block of code uses Array.Sort to order a list of Integer values.

     Public Sub SortArray(  )        ' ----- Simple array sorting example.        Dim counter As Integer        Dim dataToFix(  ) As Integer = {9, 8, 12, 4, 5}        ' ----- First, show the world the mixed-up mess.        Console.WriteLine("Unsorted:")        For counter = 0 To 4           Console.WriteLine(CStr(dataToFix(counter)))        Next counter        ' ----- Yeah! I don't have to Bubble sort by myself.        Array.Sort(dataToFix)        ' ----- Display the correct results.        Console.WriteLine("Sorted:")        For counter = 0 To 4           Console.WriteLine(dataToFix(counter))        Next counter     End Sub 

The output is:

     Unsorted:     9     8     12     4     5     Sorted:     4     5     8     9     12 

Some of the more important methods of the Array class are shown in Table 7-2.

Table 7-2. Some members of the System.Array class

Method

Description

BinarySearch

Searches a sorted one-dimensional array for a value

IndexOf

Returns the location of the first occurrence of a particular value in a one-dimensional array

LastIndexOf

Returns the location of the last occurrence of a particular value in a one-dimensional array

Reverse

Reverses the order of the elements in a one-dimensional array or a portion of a one-dimensional array

Sort

Sorts a one-dimensional array


New in 2005. Beginning with the 2.0 release of the .NET Framework, System.Array now includes features that support the new generics functionality, including a wrapper for read-only, type-specific arrays. For information on using generics, see Chapter 10.

7.1.3. The System.Math Class

The System.Math class includes a number of mathematical methods (such as trigonometric functions), as well as some more useful general numeric methods, such as Max and Min. For instance, to determine the maximum of two values, use:

     MsgBox("The maximum of 4 and 7 is " & Math.Max(4, 7)) 

Table 7-3 shows the members of the Math class.

Table 7-3. The members of the System.Math class

Topic

Description

Abs Method

Absolute value

Acos Method

Arccosine

Asin Method

Arcsine

Atan Method

Arctangent; returns the angle with the tangent that is a specified number

Atan2 Method

Arctangent; returns the angle with the tangent that is the quotient of two specified numbers

BigMul Method

Multiplies two large 32-bit integers, returning a 64-bit integer

Ceiling Method

Returns the smallest integer greater than or equal to the argument

Cos Method

Cosine

Cosh Method

Hyperbolic cosine

DivRem Method

Returns the modulus, that is, the remainder of a division operation

E Field

The natural number e

Exp Method

The natural number e raised to a power

Floor Method

Returns the largest integer less than or equal to the argument

IEEERemainder Method

Returns the remainder of a division operation using an IEEE-defined standard function

Log Method

Natural (base e) logarithm

Log10 Method

Common (base 10) logarithm

Max Method

Maximum of two values

Min Method

Minimum of two values

Pi Field

p, the ratio of the circumference of a circle to its diameter

Pow Method

Exponentiation function

Round Method

Rounds a given number to a specified number of decimal places

Sign Method

Determines the sign of a number

Sin Method

Sine

Sinh Method

Hyperbolic sine

Sqrt Method

Square root of a value

Tan Method

Tangent

Tanh Method

Hyperbolic tangent

Truncate Method

Returns the integral portion of a number


7.1.4. The System.String Class

The System.String class implements a set of string manipulation features, including methods for substring isolation, concatenation, replacement, padding, trimming, and so on.

The VB String data type is equivalent to the System.String class, so the methods of System.String apply directly to VB strings, as with the Insert method:

     Dim famousQuote As String = "To be to be"     MsgBox(famousQuote.Insert(6, "or not ")) 

This displays the message, "To be or not to be."

In .NET, strings are immutable. That is, they cannot be modified once they are created. All methods of the String class that make changes to strings actually create a new instance of a string that contains the changes.

Table 7-4 shows some significant members of the System.String class.

Table 7-4. Some members of the System.String class

Member

Description

Chars Property

Returns the character at a specific position

Compare Method

Compares two string objects

CompareTo Method

Compares a string with a designated object

Concat Method

Concatenates one or more strings

Contains Method

Indicates whether a string contains a certain substring

Copy Method

Creates a new copy of an existing string

CopyTo Method

Copies characters from a string into a character array

Empty Field

A read-only field that represents an empty string

EndsWith Method

Indicates whether the end of a string matches a specified string

Equals Method

Determines whether a string is equal to another string

Format Method

Returns a new string built from a patterned format of one or more data objects

IndexOf Method

Returns the position of the first occurrence of a substring within a string

IndexOfAny Method

Returns the position within a string of the first occurrence of any character from a given set of characters

Insert Method

Inserts a substring into a string

Join Method

Concatenates each element of a string array with a specific delimiter between each original string, and returns a new string with the result

LastIndexOf Method

Returns the position of the last occurrence of a substring within a string

LastIndexOfAny Method

Returns the position within a string of the last occurrence of any character from a given set of characters

Length Property

Returns the number of characters in a string

PadLeft Method

Right-aligns the characters in a string

PadRight Method

Left-aligns the characters in a string

Remove Method

Deletes a specified number of characters from a string starting at a specific position

Replace Method

Replaces all occurrences of a substring in a string with another substring

Split Method

Splits a delimited string into an array of strings

StartsWith Method

Indicates whether the beginning of a string matches a particular substring

Substring Method

Extracts a substring from a string by position

ToCharArray Method

Copies the characters of a string to a character array

ToLower Method

Converts a string to lowercase

ToUpper Method

Converts a string to uppercase

Trim Method

Removes all occurrences of a set of characters (usually whitespace characters) from the beginning and end of a string

TrimEnd Method

Removes all occurrences of a set of characters (usually whitespace characters) from the end of a string

TrimStart Method

Removes all occurrences of a set of characters (usually whitespace characters) from the beginning of a string





Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net