I have designed a private collection to hold filter values. This collection is simply a bunch of class objects that hold other data .... it is built from reading a file, as shown below.
Public Class Utility
Private mFilterCollection As New Collection
Private Sub LoadFilterList()
'Open the file stream
Dim fs As IO.FileStream = New IO.FileStream(My.Application.Info.DirectoryPath & "/file.flt", IO.FileMode.OpenOrCreate, IO.FileAccess.Read)
Dim sr As New IO.StreamReader(fs)
Dim N As Integer
Dim mString As String
Dim mFilter As SelectionFilterCollection
Dim varValues As String()
'Seek the beginning of the file
sr.BaseStream.Seek(0, IO.SeekOrigin.Begin)
'Create our class to hold the filter information
'this is necessary at this point to aleviate
'a warning that an uninitialized variable may
'cause an error
mFilter = New SelectionFilterCollection
While sr.EndOfStream <> True
mString = sr.ReadLine()
'If we have a header i.e. filter name
If mString.Substring(0, 3) = "***" Then
'Create a new SelectionFilterCollection to hold it
'recycling the previous mFilter
'We need to do this each time a new filter is found
'in the file stream
mFilter = New SelectionFilterCollection
'Add the filter name to the combo box
Me.ComboBox1.Items.Add(mString.Substring(3))
'Set the Filter collection name to the same name so we
'can identify it later when it is selected in the
'ComboBox
mFilter.Name = mString.Substring(3)
Else
'We don't have a filter header, so we must have filter
'information in tab delimited format
'so split it into the 3 parts
varValues = Split(mString, vbTab)
'and add those parts to our filter items
'See the SelectionFilterCollection Class for information on its structure
mFilter.Add(varValues(0), varValues(1), varValues(2))
End If
'If we have anything in the collection already ...
If mFilterCollection.Count > 0 Then
'make sure it isn't duplicated ...
If mFilterCollection.Item(mFilter.Name.ToString) Is Nothing Then
'then add it if needed
mFilterCollection.Add(mFilter, mFilter.Name)
End If
Else
'otherwise just add it
mFilterCollection.Add(mFilter, mFilter.Name)
End If
End While
'release the filter
mFilter = Nothing
'close the file stream
sr.Close()
End Sub
End Class
Class SelectionFilterCollection
'This class is designed to hold multiple
'block filters as an array of items
'FilterItem is a class that holds a block name
'attribute tag and a segregation value
'Define public members
Public Name As String
Public Item As FilterItem()
'New method
Public Sub New()
Name = ""
End Sub
'Add method
Public Function Add(ByVal BlockName As String, ByVal Attribute As String, ByVal Segregate As String) As FilterItem
If Item Is Nothing = True Then
ReDim Item(0)
Else
ReDim Preserve Item(UBound(Item) + 1)
End If
Dim NItem As New FilterItem
NItem.BlockName = BlockName
NItem.Attribute = Attribute
NItem.Segregate = Segregate
Item(UBound(Item)) = NItem
Return NItem
End Function
'Count method
Public Function Count() As Integer
If Item Is Nothing = True Then
Return 0
Else
Return (UBound(Item) + 1)
End If
End Function
End Class
Class FilterItem
'This class is designed to hold a block name
'an attribute associated with that block and
'whether or not the blocks should be identified
'seperately based on the attribute value
'Define public members
Public BlockName As String
Public Attribute As String
Public Segregate As String
'New method
Public Sub New()
BlockName = ""
Attribute = ""
Segregate = ""
End Sub
End Class
This part seems to work correctly, as there are no errors generated (I removed all error checking to ensure it would break when running though the paces ...
However, when I change the combobox value (I added all of the filters in the above code) it crashes when I select the last item in the collection ...
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim mFilter As SelectionFilterCollection
Dim mListItem As ListViewItem
'If we don't have an unnamed filter
'and if we are not saving additional
'data to the collection
If IsUnnamed() = False And mSavingData = False Then
'Clear the listview showing the filter criteria
If Me.ListView1.Items.Count > 0 Then
Me.ListView1.Items.Clear()
End If
'Set the filter collection to the item referenced
'in the collection, searching by "key name" (override 2)
mFilter = mFilterCollection.Item(Me.ComboBox1.Text)
'If we have a filter
If mFilter Is Nothing = False Then
'loop through each one in the collection of items
For Each fItem As FilterItem In mFilter.Item
'putting the data in the listview
mListItem = Me.ListView1.Items.Add(fItem.BlockName)
mListItem.SubItems.Add(fItem.Attribute)
mListItem.SubItems.Add(fItem.Segregate)
'release the list item
mListItem = Nothing
Next
End If
End If
End Sub
The program crashes and burns here saying that the object is not set .. I am not sure why ...
mFilter = mFilterCollection.Item(Me.ComboBox1.Text)
but only if I select the last item in the collection ... if there are 4 items, I can select the first three without a problem
Ideas?
Am I not putting the last item in the collection when I build it?
Is there an easier way?
Ok .. ignore that last question ... sure there is an easier way ... 'cause I ain't using it