The Open Source Swiss Army Knife

/code/vb/
/code/vb/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /vb 
Permalink: arrays.htm
Title: add
article options : please login   |  print view

  1. first exmaple
  2. returning a list value from a function
  3. arrayListJoin
  4. filterDuplicates

first

Sub main()
    dim nElements as Integer
    nElements = 10

    ' Static array of variable size
    Dim someInts(nElements - 1) As Integer
    someInts(0) = 1
   
    ' Static array of 10 elements, index 1-10
    Dim moreInts(1 To 10) As Integer
    moreInts(1) = 2
   
    ' Static 2-dimensional array, index 0-2, 0-2
    Dim fourInts(2, 2) As Integer
    fourInts(0, 0) = 3
   
    ' Dynamic array of 10 elements
    Dim dynaInts() As Integer
    ReDim dynaInts(9)
    dynaInts(9) = 4
   
    ' Resizing the array to hold twice as much
    ReDim Preserve dynaInts(19)
    ' dynaInts(9) is still 4
End Sub

return

Sometimes it's necessary to break up strings into smaller strings, for example if you wanted to submit a sentence as a list of words to a database query. The Split function allows you to break a string into an array, based on a delimiter. The first parameter to the Split function is the string, the second parameter is the delimiter. The function returns an array of the elements that have been split out of the string. Using a delimiter of a space will break out the words. The following example writes each word in a TextBox called txtSentence and writes the words down the form.

    Dim list As Variant
    Dim strSentence As String
    Dim counter As Integer
    strSentence = txtSentence.Text
    list = Split(strSentence, " ")
    For counter = 0 To UBound(list)
        Me.Print list(counter)
    Next counter

arrayListJoin

' A reusable function that merges two ArrayList objects
' Example:
'     Dim al As New ArrayList()
'     al.Add("Jack")
'     al.Add("Mary")
'     val.Add("Bob")
'     al.Add("Tom")
'    
'     Dim al2 As New ArrayList()
'     al2.Add("Frank")
'     al2.Add("Lee")
'     al2.Add("Nick")
'    
'     al = ArrayListJoin(al, al2)

Function ArrayListJoin(ByVal al1 As ArrayList, ByVal al2 As ArrayList) As _
    ArrayList
    ' Note how we avoid time-consuming reallocations.
    ArrayListJoin = New ArrayList(al1.Count + al2.Count)
    ' Append the items in the two ArrayList arguments.
    ArrayListJoin.AddRange(al1)
    ArrayListJoin.AddRange(al2)
End Function


' Note: this code is taken from Francesco Balena's "Programming Microsoft
' Visual Basic .NET" book (MS Press 2002)

filterDuplicates

' Filter out duplicate values in an array and compact
' the array by moving items to "fill the gaps".
' Returns the number of duplicate values
'
' The array is not REDIMed, but you can do it easily using
' the following code:
'     a() is a string array
'     dups = FilterDuplicates(a())
'     If dups Then
'         ReDim Preserve a(LBound(a) To UBound(a) - dups) As String
'     End If

Function FilterDuplicates(ByVal arr As Array) As Integer
    Dim index As Integer
    Dim dups As Integer
    ' we use a hashtable to track duplicates
    Dim ht As New Collections.Hashtable(arr.Length * 2)

    For index = 0 To arr.Length - 1
        Dim value As Object = arr.GetValue(index)

        If ht.Contains(value) Then
            ' we've found a duplicate
            arr.SetValue(Nothing, index)
            dups += 1
        Else
            ' add to the hashtable (use a dummy Nothing value)
            ht.Add(value, Nothing)
            ' if we've found one or more duplicates so far
            ' we need to move elements towards lower indices
            If dups > 0 Then
                arr.SetValue(value, index - dups)
                arr.SetValue(Nothing, index)
            End If
        End If
    Next

    ' return the number of duplicates
    Return dups

End Function

Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top