Using collections to add sub procedures to class properties

I've been encountering some difficulties describing my question and with the responses I have (and haven't) been getting, so I'm askng this question again, another way, hoping to elicit more, constructive responses. Hopefully this time I've communicated my problem clearly enough. Here it is:

I have a class with an property that is a string array (named FileNames) for storing filenames. As my code is now, I instantiate the class (as ProjectName, for example) in a form and add filenames to the class' FileNames array by using:

ReDim Preserve ProjectName.FileNames(ProjectName.FileNames.Length)
ProjectName.FileNames(ProjectName.FileNames.Length - 1) = "new filename to add"

This works fine, but I'd rather the class itself handle additions and removals to the list of filenames it stores. Instead of using ReDim, I'd like to interact with the class from my form the same way ListView and other controls are used. For example, to add and remove a filename, I'd like to use:

ProjectName.FileNames.Add("new filename to add")
ProjectName.FileNames(Index).Remove()

I don't know how to do this though. I believe this requires somehow using collections, but, as a self-taught VB4 - 6 programmer trying to learn .NET, I haven't encountered collections before, and my Googling efforts haven't turned anything up. Could someone please tell me how to accomplish this?

Thank-you.

- Chris O.
ineedyourhelpAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Basically you would wrap an ArrayList in a class as shown below.  Just add subs/functions to your class to "wrap" whatever subs/functions you want to publicly expose in your class:

Public Class FileNames

    Private _FileNames As New ArrayList

    Public Sub Add(ByVal FileName As String)
        _FileNames.Add(FileName)
    End Sub

    Default Public Property Item(ByVal index) As String
        Get
            If index >= 0 And index < _FileNames.Count Then
                Return _FileNames.Item(index)
            Else
                MessageBox.Show("Index = " & index & vbCrLf & "Count = " & Me.Count, "Index Out of Bounds", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Get
        Set(ByVal Value As String)
            If index >= 0 And index < _FileNames.Count Then
                _FileNames.Item(index) = Value
            Else
                MessageBox.Show("Index = " & index & vbCrLf & "Count = " & Me.Count, "Index Out of Bounds", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Set
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return _FileNames.Count
        End Get
    End Property

    Public Sub Remove(ByVal Index As Integer)
        If Index >= 0 And Index < _FileNames.Count Then
            _FileNames.RemoveAt(Index)
        Else
            MessageBox.Show("Index = " & Index & vbCrLf & "Count = " & Me.Count, "Index Out of Bounds", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Public Sub Clear()
        _FileNames.Clear()
    End Sub

    Public Function Contains(ByVal value As String) As Boolean
        Return _FileNames.Contains(value)
    End Function

End Class
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.