Section 6.1. The System Namespace

   

6.1 The System Namespace

The System namespace contains classes for such broad ranging things as:

  • Data types

  • Data type conversions

  • Method-parameter manipulation

  • Events and event handlers

  • Mathematics

  • Program invocation

  • Application-environment management

6.1.1 Data Type Conversion

To illustrate data type conversion, the System namespace defines a class called Convert. If you check the documentation, you'll find that one of the methods of the Convert class is ToBoolean. The documentation lists the following for ToBoolean:

 Overloads Public Shared Function ToBoolean(String) As Boolean Overloads Public Shared Function ToBoolean(Double) As Boolean Overloads Public Shared Function ToBoolean(Single) As Boolean Overloads Public Shared Function ToBoolean(Char) As Boolean Overloads Public Shared Function ToBoolean(Byte) As Boolean Overloads Public Shared Function ToBoolean(Object) As Boolean Overloads Public Shared Function ToBoolean(Boolean) As Boolean Overloads Public Shared Function ToBoolean(Long) As Boolean Overloads Public Shared Function ToBoolean(Integer) As Boolean 

As you can see, there are many ToBoolean functions ” each one with a different argument signature ” to take care of converting various data types to Boolean.

Now, just for exercise, we can write:

 Dim s As String Dim b As Boolean s = "false" b = System.Convert.ToBoolean(s) msgbox(b) 

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:

 b = Convert.ToBoolean(s) 

Of course, we can also use the built-in VB.NET function CBool .

The Convert class contains methods for converting data to the standard Visual Basic data types, as well as to the data types supported by the .NET Framework but not wrapped by Visual Basic, such as the unsigned-integer data types. The most important of these methods are shown in Table 6-1.

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

Method

Description

ToBoolean

Converts a value to a Boolean

ToByte

Converts a value to a Byte

ToChar

Converts a value to a Char

ToDateTime

Converts a value to DateTime (Date in Visual Basic)

ToDecimal

Converts a value to Decimal

ToDouble

Converts a value to Double

ToInt16

Converts a value to Int16 (Short in Visual Basic)

ToInt32

Converts a value to Int32 (Integer in Visual Basic)

ToInt64

Converts a value to Int64 (Long in Visual Basic)

ToSByte

Converts a value to SByte, the unsigned-byte data type in the BCL

ToSingle

Converts a value to Single

ToString

Converts a value to String

ToUInt16

Converts a value to UInt16, an unsigned 16-bit integer

ToUInt32

Converts a value to UInt32, an unsigned 32-bit integer

ToUInt64

Converts a value to UInt64, an unsigned 64-bit integer

6.1.2 The Array Class

The Array class contains useful methods for dealing with arrays. For instance, the Array object has a Sort method (at last) that sorts the elements of an array. Here is an example:

 Sub sortArray(  ) Dim i As Integer Dim intArray(  ) As Integer = {9, 8, 12, 4, 5} For i = 0 To 4     console.WriteLine(CStr(intArray(i))) Next Array.Sort(intarray) Console.WriteLine("Sorted:") For i = 0 To 4     console.WriteLine(intArray(i)) Next End Sub 

The output is:

 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 6-2.

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

Method

Description

BinarySearch

Searches a sorted one-dimensional array for a value

IndexOf

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

LastIndexOf

Returns 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

6.1.3 The Math Class

The Math class has a number of mathematical-function methods (such as trigonometric functions), as well as some more useful methods, such as Max and Min. Therefore, we can just write:

 MsgBox(Math.Max(4,7)) 

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

Table 6-3. The members of the Math class

Topic

Description

Abs function

Absolute value

Acos function

Arccosine

Asin function

Arcsine

Atan function

Arctangent; returns the angle whose tangent is a specified number

Atan2 function

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

Ceiling function

Returns the smallest integer greater than or equal to the argument number

Cos function

Cosine

Cosh function

Hyperbolic cosine

E field

The natural number e

Exp function

Exponential function

Floor function

Returns the largest integer less than or equal to the argument number

IEEERemainder function

Returns the remainder after dividing x by y

Log function

Natural (base e) logarithm

Log10 function

Common (base 10) logarithm

Max function

Maximum

Min function

Minimum

Mod operator

Returns the modulus , that is, the remainder when number1 is divided by number2

Pi field

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

Pow function

Generalized exponential function

Randomize statement

Initializes the random number generator

Rnd function

Returns a random number

Round function

Rounds a given number to a specified number of decimal places

Sign function

Determines the sign of a number

Sin function

Sine

Sinh function

Hyperbolic sine

Sqrt function

Square root

Tan function

Tangent

Tanh function

Hyperbolic tangent

6.1.4 The String Class

The String class implements a collection of methods for string manipulation, including methods for locating substrings, concatenation, replacement, padding, trimming, and so on. One interesting method is Insert, which inserts a new string into an existing string.

The VB.NET String data type is equivalent to the System.String class, so we can apply the methods of System.String directly to VB strings, as in:

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

This displays the message "To be or not to be." Table 6-4 shows the members of the String class.

Table 6-4. The members of the String class

Member

Description

Chars property

Returns the character at a specified character position in the string

Clone method

Returns a reference to an instance of the string

Compare method

A shared method that compares two string objects

CompareOrdinal method

A shared method that compares two string objects without considering localization

CompareTo method

Compares a string with a designated object

Concat method

Concatenates one or more instances of string

Copy method

A shared function that creates a new instance of a string with the same content as a designated string

CopyTo method

Copies a number of characters from a string to a specified position in an array of Unicode characters

Empty field

A read-only field that represents an empty string.

EndsWith method

Determines whether the end of a string matches a specified string

Equals method

Determines whether the string is equal to another string

Format method

Replaces a format specification with its textual equivalent

IndexOf method

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

IndexOfAny method

Returns the position of the first occurrence in a string of any of a set of characters

Insert method

Inserts a substring into a string

Join method

A shared method that concatenates a string separator and the elements of a string array

LastIndexOf method

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

LastIndexOfAny method

Returns the position of the last occurrence in a string of any of a set of characters

Length property

Returns the number of characters in the 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 particular position

Replace method

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

Split method

Splits a delimited string into a string array

StartsWith method

Determines whether the beginning of a string matches a particular substring

Substring method

Extracts a substring from a string

ToCharArray method

Copies the characters in 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 from the beginning and end of a string

TrimEnd method

Removes all occurrences of a set of characters from the end of a string

TrimStart method

Removes all occurrences of a set of characters from the beginning of a string

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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