The Array class has been expanded with many generic methods. For example, consider the following code:
Using the Array.ConvertAll method and an anonymous method you can simplify the conversion loop as follows:
Surprisingly, however, you can simplify this code even further and even render it with VB 2005 (which doesn't support anonymous methods). The trick is to find a static method in the .NET Framework that takes a number and returns the argument's hex value. Strictly speaking, the .NET Framework doesn't expose a type with such a method, but you can use the Hex method of the Microsoft.VisualBasic.Conversion type:
// this code requires a reference to the Microsoft.VisualBasic.dll
' This the VB versionDim strArray As String() = Array.ConvertAll(Of Integer, String)(intArray, AddressOf Hex)
The Visual Basic library exposes a few other methods that you can use in this fashion, for example UCase, LCase, Trim, LTrim, RTrim, Int, Val, Asc, Chr, Len. You can find other useful methods everywhere in the .NET Framework, for example the Convert class.
Likewise, you can replace the loop that displays the results to the console window with a simpler Array.ForEach method
// C#Array.ForEach<string>(strArray, Console.WriteLine);
' VBArray.ForEach(Of String)(strArray, AddressOf Console.WriteLine)
Remember Me
Powered by: newtelligence dasBlog 1.8.5223.1