Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pulling items out of an Array

Posted on 2004-10-18
3
Medium Priority
?
263 Views
Last Modified: 2010-04-24
If I have MyArray(100) and wanted to pull out all elements that begin with a particular letter, how would that be done?  
0
Comment
Question by:Jedidia
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12340007
Here is one approach:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private myArray() As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Put some test data in our array
        Dim temp As String
        temp = "cat,dog,elephant,cougar,crab,bear,fish,camel"
        myArray = Split(temp, ",")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim match As String
        Dim matches As ArrayList = getEntries("c")

        For Each match In matches
            ' do something with each match
            Debug.WriteLine(match)
        Next
    End Sub

    Private Function getEntries(ByVal beginningWith As String) As ArrayList
        Dim entries As ArrayList = New ArrayList
        Dim i As Integer

        For i = 0 To myArray.GetUpperBound(0)
            If myArray(i).StartsWith(beginningWith) Then
                entries.Add(myArray(i))
            End If
        Next i

        Return entries
    End Function

End Class
0
 
LVL 4

Expert Comment

by:gdexter
ID: 12340116
This Function will return an ArrayList containing the elements starting with the searchParam variable

    Private Function GetElements(ByVal searchParam As String) As ArrayList

        Dim i As Integer
        Dim al As New ArrayList

        For i = 0 To MyArray.GetUpperBound(0)
            If (CType(MyArray(i), String).StartsWith(searchParam)) Then
                al.Add(MyArray(i))
            End If
        Next

        Return al
    End Function
0
 
LVL 4

Expert Comment

by:gdexter
ID: 12340131
To late...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

824 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