?
Solved

Using collections to add sub procedures to class properties

Posted on 2005-04-26
1
Medium Priority
?
198 Views
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")
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.
0
Comment
Question by:ineedyourhelp
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 13873045
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

757 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