Using collections to add sub procedures to class properties

Posted on 2005-04-26
Last Modified: 2010-04-23
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")

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?


- Chris O.
Question by:ineedyourhelp
    1 Comment
    LVL 85

    Accepted Solution

    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)
        End Sub

        Default Public Property Item(ByVal index) As String
                If index >= 0 And index < _FileNames.Count Then
                    Return _FileNames.Item(index)
                    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
                    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
                Return _FileNames.Count
            End Get
        End Property

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

        Public Sub Clear()
        End Sub

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

    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now