Solved

Pulling items out of an Array

Posted on 2004-10-18
3
215 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 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
To late...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

13 Experts available now in Live!

Get 1:1 Help Now