tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
1
ZUGRIFFE
1221
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Trash Trash ist offline Mitglied Brokat
    Registriert seit
    Jan 2002
    Beiträge
    400
    moin,
    ich habe eine Funktion in VBA geschrieben, die mir in bestimmte Wörter in Form eines Arrays zurückliefern soll. Da ich die Anzahl der Wörter nicht kenne, würde ich eine ArrayList verwenden. Wenn ich allerdings die ArrayList in ein Array umforme und zurückgeben möchte, bekomme ich irgendwelche "unverträglichen Typen" zurück... In der objArrayList dürften eigentlich laut Debugger nur Strings sein.

    Muss ich die objArrList irgendwie "casten"?

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    Private Function getLocationNames(location As String, length_of_location As Integer, length_of_left As Integer) As String()
     
    'ArrayList
    Dim objArrLst
    Set objArrLst = CreateObject("System.collections.arraylist")
    Dim arr() As String
     
    .
    . [weggelassen]
    .
     
     
    If foundSomething <> foundSomething_temp Then
    objArrLst.Add foundSomething
    End If
     
    arr = objArrLst.ToArray
    getLocationNames = arr
    End Function

    Danke!
     

  2. #2
    Avatar von Yaslaw
    Yaslaw Yaslaw ist offline n/a
    tutorials.de Moderator
    Registriert seit
    Dec 2007
    Ort
    Winterthur(CH)
    Beiträge
    5.205
    Sehe ich das Richtig, du brauchst die ArrayList nur damit du ein flexibles Array hast, das du mir ADD erweitern kannst ohne das du jedesmal ein redim durchführen musst?

    Eine kleine Funktionsammlung ermöglicht dir dieses relativ einfach mit dem normalen Array.

    Code vb:
    1
    
    call pushArray(myArray, newValue)

    Und hier meine Sammlung die ich eigentlich in jedem VB(A)-Projekt brache
    Code vb:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    
    '-------------------------------------------------------------------------------
    'File         : ArrayFunctions
    '               mpl © by ERB software
    '               All rights reserved
    'Environment  : Access 2000/XP, VB6
    'Version      : 1.0
    'Name         : Array Funktionen
    'Description  : Diverse nützliche Funktionen zu Array's
    'Author       : Stefan Erb (ERS)
    'History      : 18.10.2004 - ERS - Creation
    '-------------------------------------------------------------------------------
     
    Option Explicit
    Option Compare Database
     
    '-------------------------------------------------------------------------------
    ' Public Methodes
    '-------------------------------------------------------------------------------
    '   /**
    '    * IsEmptyArray
    '    * prüft ob ein Array initializiert ist
    '    * @param   Array
    '    * @return  true if Array is not initialized
    '    */
    Public Function IsEmptyArray(ByVal iArray As Variant) As Boolean
      Dim Dummy As Long
     
      If IsArray(iArray) Then
     
        'Ggf. Fehler provozieren:
        On Error Resume Next
        Dummy = LBound(iArray)
     
        'Ergebnis bestimmen:
        IsEmptyArray = (Err.Number <> 0)
        On Error GoTo 0
     
      Else
     
        Err.Raise 13 'Type mismatch'
     
      End If
    End Function
     
    '   /**
    '    * addArray
    '    * Add Array2 to Array1
    '    * @param   Array1
    '    * @param   Array2
    '    */
    Public Sub addArray(ByRef ioArray As Variant, ByVal iArray As Variant)
        Dim i
        If Not IsEmptyArray(iArray) Then
            For i = 0 To UBound(iArray)
                Call pushArray(ioArray, iArray(i))
            Next i
        End If
        
    End Sub
     
    '   /**
    '    * pushArray
    '    * add Value to the Array
    '    * @param   Array
    '    * @param   Value
    '    * @return  Ubound of the Array
    '    */
    Public Function pushArray(ByRef ioArray As Variant, ByVal iValue As Variant) As Long
      
      Call ReDimArray(ioArray:=ioArray, oUbound:=pushArray)
      ioArray(pushArray) = iValue
     
    End Function
     
    '   /**
    '    * ArrayIndex
    '    * search the index of a element in a array
    '    * @param   element
    '    * @param   array
    '    * @return  index of the array
    '    */
    Public Function ArrayIndex( _
        ByRef iElement As Variant, _
        ByRef iArray As Variant _
      ) As Long
     
      If IsEmptyArray(iArray) Then
        ArrayIndex = -1
        Exit Function
      End If
      If IsObject(iElement) Then
     
        'Objekte vergleichen:
        For ArrayIndex = LBound(iArray) To UBound(iArray)
          If IsObject(iArray(ArrayIndex)) _
          Then If iElement Is iArray(ArrayIndex) _
               Then Exit Function
        Next ArrayIndex
       
      Else
     
        '"Normale" Werte vergleichen:
        For ArrayIndex = LBound(iArray) To UBound(iArray)
          If Not IsObject(iArray(ArrayIndex)) _
          Then If iElement = iArray(ArrayIndex) _
               Then Exit Function
        Next ArrayIndex
     
      End If
     
      'Kein Treffer:
      ArrayIndex = LBound(iArray) - 1
    End Function
     
    '   /**
    '    * ReDimArray
    '    * redim a array
    '    * @param   array (ByRef)
    '    * @param   step
    '    * @param   Ubound of the Array
    '    * @return  new array
    '    */
    Public Function ReDimArray( _
            ByRef ioArray As Variant, _
            Optional ByVal iStep As Long = 1, _
            Optional ByRef oUbound As Long) _
    As Variant
     
      If IsEmptyArray(ioArray) Then
        ReDim ioArray(iStep - 1)
      ElseIf UBound(ioArray) + iStep > -1 Then
        ReDim Preserve ioArray(UBound(ioArray) + iStep)
      End If
      ReDimArray = ioArray
      oUbound = UBound(ioArray)
     
    End Function
     
    '   /**
    '    * ArrayQuickSort
    '    * sort the elements of a array
    '       [url]http://www.vbarchiv.net/archiv/tipp_details.php?pid=372[/url]
    '    * @param   array (ByRef)
    '    * @param   start
    '    * @param   end
    '    */
    Public Sub ArrayQuickSort(ByRef ioArray As Variant, _
      Optional ByVal iStart As Variant, _
      Optional ByVal iEnd As Variant)
     
      ' Wird die Bereichsgrenze nicht angegeben,
      ' so wird das gesamte Array sortiert
      
      If IsMissing(iStart) Then iStart = LBound(ioArray)
      If IsMissing(iEnd) Then iEnd = UBound(ioArray)
      
      Dim i As Long
      Dim j As Long
      Dim h As Variant
      Dim x As Variant
      
      i = iStart: j = iEnd
      x = ioArray((iStart + iEnd) / 2)
      
      ' Array aufteilen
      Do
      
        While (ioArray(i) < x): i = i + 1: Wend
        While (ioArray(j) > x): j = j - 1: Wend
            
        If (i <= j) Then
          ' Wertepaare miteinander tauschen
          h = ioArray(i)
          ioArray(i) = ioArray(j)
          ioArray(j) = h
          i = i + 1: j = j - 1
        End If
      Loop Until (i > j)
      
      ' Rekursion (Funktion ruft sich selbst auf)
      If (iStart < j) Then ArrayQuickSort ioArray, iStart, j
      If (i < iEnd) Then ArrayQuickSort ioArray, i, iEnd
    End Sub
     
    ---------------------------------------------------------------------------------------------------
    item: Ich habe es mir aus gesundheitlichen Gründen abgewöhnt unformatierten Code zu lesen (Auch SQL-Statements kann man formatieren!)
    item: Tutorial: [PHP][MySQL] Debug Queries
    item: Schreibt mir keine PN mit Fragen die im Forum beantwortet werden können - ich mache kein persönliches coaching
    item: Bitte zur besseren Lesbarkeit PHP-Code in [PHP]...[/PHP], SQL in [SQL]...[/SQL], Visual Basic in [VB]...[/VB] etc. schreiben

Ähnliche Themen

  1. PHP Funktion Array zurückgeben
    Von chriss_2oo4 im Forum PHP
    Antworten: 6
    Letzter Beitrag: 25.01.10, 18:35
  2. Array => Key zurückgeben
    Von AtotheZ im Forum PHP
    Antworten: 0
    Letzter Beitrag: 24.01.09, 11:56
  3. arraylist in array umwandeln
    Von drummingmichel im Forum Algorithmen & Datenstrukturen mit Java
    Antworten: 3
    Letzter Beitrag: 20.01.09, 09:47
  4. C# - ArrayList in generische Liste umwandeln
    Von janw im Forum .NET Café
    Antworten: 1
    Letzter Beitrag: 05.03.08, 15:04
  5. Array aus Funktion zurückgeben
    Von Schnüdde im Forum Visual Basic 6.0
    Antworten: 2
    Letzter Beitrag: 06.12.07, 09:14