|
VB Helper Newsletter
|
Rod Stephens
|
Sep 04, 2009 14:32 PDT
|
Have a great week and thanks for subscribing!
----------
Rod
RodSte-@vb-helper.com
Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
VB.NET Contents:
1. New HowTo: Use an IComparer class to sort a file using the values in
its columns
2. New HowTo: Read and write all of the lines in a text file in Visual
Basic .NET
3. New HowTo: Initialize an array with a range of values or a repeated
value in Visual Basic .NET (version 2008 or later)
==========
++++++++++
<VB.NET>
++++++++++
==========
1. New HowTo: Use an IComparer class to sort a file using the values in
its columns
http://www.vb-helper.com/howto_net_sort_file_by_columns.html
http://www.vb-helper.com/HowTo/howto_net_sort_file_by_columns.zip
The Array class's Sort method can sort just about anything if you tell
it how to compare two objects of the type you're sorting. The idea is to
make a class that implements IComparer and pass an instance to
Array.Sort so it knows how to sort the lines in the file.
The ColumnSorter class implements IComparer(Of String) so it can compare
two strings. The constructor takes a list of the columns that should be
used to compare two lines from the file. If a column index is negative,
that means the sorter should sort the column in descending order.
The really interesting part of the class is the Compare function, which
compares two rows from the file. In this case, it splits the lines into
space-separated fields and compares the correct column values.
Public Class ColumnSorter
Implements IComparer(Of String)
Private m_Columns() As Integer
' Save the column numbers for sorting.
Public Sub New(ByVal ParamArray numbers() As Integer)
m_Columns = numbers
End Sub
' Compare based on the columns.
Public Function Compare(ByVal x As String, ByVal y As String) As
Integer Implements System.Collections.Generic.IComparer(Of
String).Compare
' Split the strings into fields.
Dim x_fields() As String = x.Split(New String() {" "},
StringSplitOptions.RemoveEmptyEntries)
Dim y_fields() As String = y.Split(New String() {" "},
StringSplitOptions.RemoveEmptyEntries)
' Compare the columns.
For i As Integer = 0 To m_Columns.GetUpperBound(0)
Dim col As Integer = m_Columns(i)
If col < 0 Then
' Sort descending.
col = -col
If x_fields(col) < y_fields(col) Then Return 1
If x_fields(col) > y_fields(col) Then Return -1
Else
' Sort ascending.
If x_fields(col) < y_fields(col) Then Return -1
If x_fields(col) > y_fields(col) Then Return 1
End If
Next i
Return 0
End Function
End Class
Once you've built the class that implements IComparer, using it is easy.
The following code reads the file's lines into a String array, makes a
ColumnSorter to sort on the second column descending and the third
column ascending (column indexes start from 0), calls Array.Sort, and
writes the result into the output file.
Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSort.Click
' Read the input file.
Dim lines() As String =
System.IO.File.ReadAllLines(txtInputFile.Text)
' Make a column sorter.
' Note: Columns start numbering from 0.
Dim column_comparer As New ColumnSorter(-1, 2)
' Sort.
Array.Sort(lines, column_comparer)
' Save the result.
System.IO.File.WriteAllLines(txtOutputFile.Text, lines)
MessageBox.Show("Done")
End Sub
The IComparer class is very flexible and you can use it to sort
practically anything from the simple strings in this example to complex
objects. For example, you could use IComparer classes to sort Employee
objects based on first and last name, employee ID, or Social Security
number.
==========
2. New HowTo: Read and write all of the lines in a text file in Visual
Basic .NET
http://www.vb-helper.com/howto_net_read_write_all_lines.html
http://www.vb-helper.com/HowTo/howto_net_read_write_all_lines.zip
This example doesn't really do much that's useful. It's just intended to
remind you that the ReadAllLines and WriteAllLines methods exist.
The System.IO.File class provides methods to read and write all of the
lines in a file to and from an array of strings. (This really should
have been included in the My namespace along with
My.Computer.FileSystem.ReadAllText and
My.Computer.FileSystem.WriteAllText.)
When you click the program's ReadLines button, the following button
reads the file's lines into the array all_lines. It then displays them
in a ListBox just so you can see that something happened.
Private Sub btnReadLines_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnReadLines.Click
Dim all_lines() As String =
System.IO.File.ReadAllLines(txtInputFile.Text)
lstItems.DataSource = all_lines
End Sub
When you click the Write Lines button, the following code calls the
WriteAllLines method to save the lines in the ListBox into an output
file.
Private Sub btnWriteLines_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnWriteLines.Click
System.IO.File.WriteAllLines(txtOutputFile.Text,
lstItems.DataSource)
Beep()
End Sub
==========
3. New HowTo: Initialize an array with a range of values or a repeated
value in Visual Basic .NET (version 2008 or later)
http://www.vb-helper.com/howto_net_initialize_array.html
http://www.vb-helper.com/HowTo/howto_net_initialize_array.zip
The Enumerable class provides shared methods that can initialize an
array to a range of values or a repeated set of values. When the program
starts, it uses the following code to make an array holding the values
101 through 120 and a second array holding 20 entries all set to 13. It
sets the DataSource of two ListBoxes to these arrays so you can see the
values.
Dim range_values() As Integer
range_values = Enumerable.Range(101, 20).ToArray()
lstRange.DataSource = range_values
Dim repeat_values() As Integer
repeat_values = Enumerable.Repeat(Of Integer)(13, 20).ToArray()
lstRepeat.DataSource = repeat_values
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|